1
1
import itertools
2
+ import warnings
2
3
3
4
import numpy
4
5
@@ -133,10 +134,17 @@ def from_iterable(cls, iterable, sparse=False, **kwargs):
133
134
134
135
@classmethod
135
136
def from_dataframe (
136
- cls , df , geom_col = None , idVariable = None , ids = None , id_order = None , ** kwargs
137
+ cls ,
138
+ df ,
139
+ geom_col = None ,
140
+ idVariable = None ,
141
+ ids = None ,
142
+ id_order = None ,
143
+ use_index = None ,
144
+ ** kwargs ,
137
145
):
138
146
"""
139
- Construct a weights object from a pandas dataframe with a geometry
147
+ Construct a weights object from a (geo) pandas dataframe with a geometry
140
148
column. This will cast the polygons to PySAL polygons, then build the W
141
149
using ids from the dataframe.
142
150
@@ -149,16 +157,24 @@ def from_dataframe(
149
157
the name of the column in `df` that contains the
150
158
geometries. Defaults to active geometry column.
151
159
idVariable : string
160
+ DEPRECATED - use `ids` instead.
152
161
the name of the column to use as IDs. If nothing is
153
162
provided, the dataframe index is used
154
- ids : list
155
- a list of ids to use to index the spatial weights object.
156
- Order is not respected from this list.
163
+ ids : list-like, string
164
+ a list-like of ids to use to index the spatial weights object or
165
+ the name of the column to use as IDs. If nothing is
166
+ provided, the dataframe index is used if `use_index=True` or
167
+ a positional index is used if `use_index=False`.
168
+ Order of the resulting W is not respected from this list.
157
169
id_order : list
158
- an ordered list of ids to use to index the spatial weights
170
+ DEPRECATED - argument is deprecated and will be removed.
171
+ An ordered list of ids to use to index the spatial weights
159
172
object. If used, the resulting weights object will iterate
160
173
over results in the order of the names provided in this
161
- argument.
174
+ argument.
175
+ use_index : bool
176
+ use index of `df` as `ids` to index the spatial weights object.
177
+ Defaults to False but in future will default to True.
162
178
163
179
See Also
164
180
--------
@@ -167,17 +183,62 @@ def from_dataframe(
167
183
"""
168
184
if geom_col is None :
169
185
geom_col = df .geometry .name
186
+
170
187
if id_order is not None :
188
+ warnings .warn (
189
+ "`id_order` is deprecated and will be removed in future." ,
190
+ FutureWarning ,
191
+ stacklevel = 2 ,
192
+ )
171
193
if id_order is True and ((idVariable is not None ) or (ids is not None )):
172
194
# if idVariable is None, we want ids. Otherwise, we want the
173
195
# idVariable column
174
196
id_order = list (df .get (idVariable , ids ))
175
197
else :
176
198
id_order = df .get (id_order , ids )
177
- elif idVariable is not None :
178
- ids = df .get (idVariable ).tolist ()
179
- elif isinstance (ids , str ):
180
- ids = df .get (ids ).tolist ()
199
+
200
+ if idVariable is not None :
201
+ if ids is None :
202
+ warnings .warn (
203
+ "`idVariable` is deprecated and will be removed in future. "
204
+ "Use `ids` instead." ,
205
+ FutureWarning ,
206
+ stacklevel = 2 ,
207
+ )
208
+ ids = idVariable
209
+ else :
210
+ warnings .warn (
211
+ "Both `idVariable` and `ids` passed, using `ids`." ,
212
+ UserWarning ,
213
+ stacklevel = 2 ,
214
+ )
215
+
216
+ if ids is None :
217
+ if use_index is None :
218
+ warnings .warn (
219
+ "`use_index` defaults to False but will default to True in future. "
220
+ "Set True/False directly to control this behavior and silence this "
221
+ "warning" ,
222
+ FutureWarning ,
223
+ stacklevel = 2 ,
224
+ )
225
+ use_index = False
226
+ if use_index :
227
+ ids = df .index .tolist ()
228
+
229
+ else :
230
+ if isinstance (ids , str ):
231
+ ids = df [ids ]
232
+
233
+ if not isinstance (ids , list ):
234
+ ids = ids .tolist ()
235
+
236
+ if len (ids ) != len (df ):
237
+ raise ValueError ("The length of `ids` does not match the length of df." )
238
+
239
+ if id_order is None :
240
+ id_order = ids
241
+
181
242
return cls .from_iterable (
182
243
df [geom_col ].tolist (), ids = ids , id_order = id_order , ** kwargs
183
244
)
@@ -227,7 +288,7 @@ def from_xarray(
227
288
Returns
228
289
-------
229
290
w : libpysal.weights.W/libpysal.weights.WSP
230
- instance of spatial weights class W or WSP with an index attribute
291
+ instance of spatial weights class W or WSP with an index attribute
231
292
232
293
Notes
233
294
-----
@@ -358,9 +419,18 @@ def from_iterable(cls, iterable, sparse=False, **kwargs):
358
419
return w
359
420
360
421
@classmethod
361
- def from_dataframe (cls , df , geom_col = None , ** kwargs ):
422
+ def from_dataframe (
423
+ cls ,
424
+ df ,
425
+ geom_col = None ,
426
+ idVariable = None ,
427
+ ids = None ,
428
+ id_order = None ,
429
+ use_index = None ,
430
+ ** kwargs ,
431
+ ):
362
432
"""
363
- Construct a weights object from a pandas dataframe with a geometry
433
+ Construct a weights object from a (geo) pandas dataframe with a geometry
364
434
column. This will cast the polygons to PySAL polygons, then build the W
365
435
using ids from the dataframe.
366
436
@@ -371,46 +441,93 @@ def from_dataframe(cls, df, geom_col=None, **kwargs):
371
441
for spatial weights
372
442
geom_col : string
373
443
the name of the column in `df` that contains the
374
- geometries. Defaults to active geometry column
444
+ geometries. Defaults to active geometry column.
375
445
idVariable : string
446
+ DEPRECATED - use `ids` instead.
376
447
the name of the column to use as IDs. If nothing is
377
448
provided, the dataframe index is used
378
- ids : list
379
- a list of ids to use to index the spatial weights object.
380
- Order is not respected from this list.
449
+ ids : list-like, string
450
+ a list-like of ids to use to index the spatial weights object or
451
+ the name of the column to use as IDs. If nothing is
452
+ provided, the dataframe index is used if `use_index=True` or
453
+ a positional index is used if `use_index=False`.
454
+ Order of the resulting W is not respected from this list.
381
455
id_order : list
382
- an ordered list of ids to use to index the spatial weights
456
+ DEPRECATED - argument is deprecated and will be removed.
457
+ An ordered list of ids to use to index the spatial weights
383
458
object. If used, the resulting weights object will iterate
384
459
over results in the order of the names provided in this
385
- argument.
460
+ argument.
461
+ use_index : bool
462
+ use index of `df` as `ids` to index the spatial weights object.
463
+ Defaults to False but in future will default to True.
386
464
387
465
See Also
388
466
--------
389
467
:class:`libpysal.weights.weights.W`
390
468
:class:`libpysal.weights.contiguity.Queen`
391
469
"""
392
- idVariable = kwargs .pop ("idVariable" , None )
393
- ids = kwargs .pop ("ids" , None )
394
- id_order = kwargs .pop ("id_order" , None )
395
470
if geom_col is None :
396
471
geom_col = df .geometry .name
472
+
397
473
if id_order is not None :
474
+ warnings .warn (
475
+ "`id_order` is deprecated and will be removed in future." ,
476
+ FutureWarning ,
477
+ stacklevel = 2 ,
478
+ )
398
479
if id_order is True and ((idVariable is not None ) or (ids is not None )):
399
480
# if idVariable is None, we want ids. Otherwise, we want the
400
481
# idVariable column
401
- ids = list (df .get (idVariable , ids ))
402
- id_order = ids
403
- elif isinstance (id_order , str ):
404
- ids = df .get (id_order , ids )
405
- id_order = ids
406
- elif idVariable is not None :
407
- ids = df .get (idVariable ).tolist ()
408
- elif isinstance (ids , str ):
409
- ids = df .get (ids ).tolist ()
410
- w = cls .from_iterable (
482
+ id_order = list (df .get (idVariable , ids ))
483
+ else :
484
+ id_order = df .get (id_order , ids )
485
+
486
+ if idVariable is not None :
487
+ if ids is None :
488
+ warnings .warn (
489
+ "`idVariable` is deprecated and will be removed in future. "
490
+ "Use `ids` instead." ,
491
+ FutureWarning ,
492
+ stacklevel = 2 ,
493
+ )
494
+ ids = idVariable
495
+ else :
496
+ warnings .warn (
497
+ "Both `idVariable` and `ids` passed, using `ids`." ,
498
+ UserWarning ,
499
+ stacklevel = 2 ,
500
+ )
501
+
502
+ if ids is None :
503
+ if use_index is None :
504
+ warnings .warn (
505
+ "`use_index` defaults to False but will default to True in future. "
506
+ "Set True/False directly to control this behavior and silence this "
507
+ "warning" ,
508
+ FutureWarning ,
509
+ stacklevel = 2 ,
510
+ )
511
+ use_index = False
512
+ if use_index :
513
+ ids = df .index .tolist ()
514
+
515
+ else :
516
+ if isinstance (ids , str ):
517
+ ids = df [ids ]
518
+
519
+ if not isinstance (ids , list ):
520
+ ids = ids .tolist ()
521
+
522
+ if len (ids ) != len (df ):
523
+ raise ValueError ("The length of `ids` does not match the length of df." )
524
+
525
+ if id_order is None :
526
+ id_order = ids
527
+
528
+ return cls .from_iterable (
411
529
df [geom_col ].tolist (), ids = ids , id_order = id_order , ** kwargs
412
530
)
413
- return w
414
531
415
532
@classmethod
416
533
def from_xarray (
@@ -457,7 +574,7 @@ def from_xarray(
457
574
Returns
458
575
-------
459
576
w : libpysal.weights.W/libpysal.weights.WSP
460
- instance of spatial weights class W or WSP with an index attribute
577
+ instance of spatial weights class W or WSP with an index attribute
461
578
462
579
Notes
463
580
-----
@@ -526,17 +643,17 @@ def Voronoi(points, criterion="rook", clip="ahull", **kwargs):
526
643
527
644
def _from_dataframe (df , ** kwargs ):
528
645
"""
529
- Construct a voronoi contiguity weight directly from a dataframe.
646
+ Construct a voronoi contiguity weight directly from a dataframe.
530
647
Note that if criterion='rook', this is identical to the delaunay
531
- graph for the points.
648
+ graph for the points if no clipping of the voronoi cells is applied.
532
649
533
650
If the input dataframe is of any other geometry type than "Point",
534
- a value error is raised.
651
+ a value error is raised.
535
652
536
653
Parameters
537
654
----------
538
655
df : pandas.DataFrame
539
- dataframe containing point geometries for a
656
+ dataframe containing point geometries for a
540
657
voronoi diagram.
541
658
542
659
Returns
@@ -561,27 +678,27 @@ def _from_dataframe(df, **kwargs):
561
678
562
679
def _build (polygons , criterion = "rook" , ids = None ):
563
680
"""
564
- This is a developer-facing function to construct a spatial weights object.
681
+ This is a developer-facing function to construct a spatial weights object.
565
682
566
683
Parameters
567
684
----------
568
685
polygons : list
569
686
list of pysal polygons to use to build contiguity
570
687
criterion : string
571
- option of which kind of contiguity to build. Is either "rook" or "queen"
688
+ option of which kind of contiguity to build. Is either "rook" or "queen"
572
689
ids : list
573
690
list of ids to use to index the neighbor dictionary
574
691
575
692
Returns
576
693
-------
577
694
tuple containing (neighbors, ids), where neighbors is a dictionary
578
695
describing contiguity relations and ids is the list of ids used to index
579
- that dictionary.
696
+ that dictionary.
580
697
581
698
NOTE: this is different from the prior behavior of buildContiguity, which
582
699
returned an actual weights object. Since this just dispatches for the
583
700
classes above, this returns the raw ingredients for a spatial weights
584
- object, not the object itself.
701
+ object, not the object itself.
585
702
"""
586
703
if ids and len (ids ) != len (set (ids )):
587
704
raise ValueError (
@@ -621,7 +738,7 @@ def buildContiguity(polygons, criterion="rook", ids=None):
621
738
This is a deprecated function.
622
739
623
740
It builds a contiguity W from the polygons provided. As such, it is now
624
- identical to calling the class constructors for Rook or Queen.
741
+ identical to calling the class constructors for Rook or Queen.
625
742
"""
626
743
# Warn('This function is deprecated. Please use the Rook or Queen classes',
627
744
# UserWarning)
0 commit comments