@@ -1326,11 +1326,33 @@ def _check_1d(x):
1326
1326
return np .atleast_1d (x )
1327
1327
else :
1328
1328
try :
1329
- ndim = x [:, None ].ndim
1330
- # work around https://github.com/pandas-dev/pandas/issues/27775
1331
- # which mean the shape is not as expected. That this ever worked
1332
- # was an unintentional quirk of pandas the above line will raise
1333
- # an exception in the future.
1329
+ # work around
1330
+ # https://github.com/pandas-dev/pandas/issues/27775 which
1331
+ # means the shape of multi-dimensional slicing is not as
1332
+ # expected. That this ever worked was an unintentional
1333
+ # quirk of pandas and will raise an exception in the
1334
+ # future. This slicing warns in pandas >= 1.0rc0 via
1335
+ # https://github.com/pandas-dev/pandas/pull/30588
1336
+ #
1337
+ # < 1.0rc0 : x[:, None].ndim == 1, no warning, custom type
1338
+ # >= 1.0rc1 : x[:, None].ndim == 2, warns, numpy array
1339
+ # future : x[:, None] -> raises
1340
+ #
1341
+ # This code should correctly identify and coerce to a
1342
+ # numpy array all pandas versions.
1343
+ with warnings .catch_warnings (record = True ) as w :
1344
+ warnings .filterwarnings ("always" ,
1345
+ category = DeprecationWarning ,
1346
+ module = 'pandas[.*]' )
1347
+
1348
+ ndim = x [:, None ].ndim
1349
+ # we have definitely hit a pandas index or series object
1350
+ # cast to a numpy array.
1351
+ if len (w ) > 0 :
1352
+ return np .asanyarray (x )
1353
+ # We have likely hit a pandas object, or at least
1354
+ # something where 2D slicing does not result in a 2D
1355
+ # object.
1334
1356
if ndim < 2 :
1335
1357
return np .atleast_1d (x )
1336
1358
return x
0 commit comments