@@ -1399,11 +1399,33 @@ def _check_1d(x):
13991399 return np .atleast_1d (x )
14001400 else :
14011401 try :
1402- ndim = x [:, None ].ndim
1403- # work around https://github.com/pandas-dev/pandas/issues/27775
1404- # which mean the shape is not as expected. That this ever worked
1405- # was an unintentional quirk of pandas the above line will raise
1406- # an exception in the future.
1402+ # work around
1403+ # https://github.com/pandas-dev/pandas/issues/27775 which
1404+ # means the shape of multi-dimensional slicing is not as
1405+ # expected. That this ever worked was an unintentional
1406+ # quirk of pandas and will raise an exception in the
1407+ # future. This slicing warns in pandas >= 1.0rc0 via
1408+ # https://github.com/pandas-dev/pandas/pull/30588
1409+ #
1410+ # < 1.0rc0 : x[:, None].ndim == 1, no warning, custom type
1411+ # >= 1.0rc1 : x[:, None].ndim == 2, warns, numpy array
1412+ # future : x[:, None] -> raises
1413+ #
1414+ # This code should correctly identify and coerce to a
1415+ # numpy array all pandas versions.
1416+ with warnings .catch_warnings (record = True ) as w :
1417+ warnings .filterwarnings ("always" ,
1418+ category = DeprecationWarning ,
1419+ module = 'pandas[.*]' )
1420+
1421+ ndim = x [:, None ].ndim
1422+ # we have definitely hit a pandas index or series object
1423+ # cast to a numpy array.
1424+ if len (w ) > 0 :
1425+ return np .asanyarray (x )
1426+ # We have likely hit a pandas object, or at least
1427+ # something where 2D slicing does not result in a 2D
1428+ # object.
14071429 if ndim < 2 :
14081430 return np .atleast_1d (x )
14091431 return x
0 commit comments