Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: fix wrong error message in deprecated 2D indexing of Series with datetime values #38099

Merged
22 changes: 22 additions & 0 deletions pandas/core/internals/blocks.py
Expand Up @@ -2393,6 +2393,28 @@ def quantile(self, qs, interpolation="linear", axis=0):
aware = self._holder(res_blk.values.ravel(), dtype=self.dtype)
return self.make_block_same_class(aware, ndim=res_blk.ndim)

def _check_ndim(self, values, ndim):
"""
ndim inference and validation.

This is overriden by the DatetimeTZBlock to check the case of 2D
data (values.ndim == 2), which should only be allowed if ndim is
also 2.
The case of 1D array is still allowed with both ndim of 1 or 2, as
if the case for other EAs. Therefore, we are only checking
`values.ndim > ndim` instead of `values.ndim != ndim` as for
consolidated blocks.
"""
if ndim is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we recently started always-passing ndim to the constructor, so should never have None here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is backported though, so prob ok (but will need a patch on 1.2)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also done in the parent method, so can use a general clean-up for 1.2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk can you open an issue so we don't forget to do this

ndim = values.ndim

if values.ndim > ndim:
raise ValueError(
"Wrong number of dimensions. "
f"values.ndim != ndim [{values.ndim} != {ndim}]"
)
return ndim


class TimeDeltaBlock(DatetimeLikeBlockMixin):
__slots__ = ()
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/series.py
Expand Up @@ -916,7 +916,8 @@ def _get_values(self, indexer):
except ValueError:
# mpl compat if we look up e.g. ser[:, np.newaxis];
# see tests.series.timeseries.test_mpl_compat_hack
return self._values[indexer]
# the asarray is needed to avoid returning a 2D DatetimeArray
return np.asarray(self._values[indexer])

def _get_value(self, label, takeable: bool = False):
"""
Expand Down
20 changes: 16 additions & 4 deletions pandas/tests/series/indexing/test_getitem.py
Expand Up @@ -389,10 +389,22 @@ def test_getitem_generator(string_series):
tm.assert_series_equal(result2, expected)


def test_getitem_ndim_deprecated():
s = Series([0, 1])
with tm.assert_produces_warning(FutureWarning):
s[:, None]
@pytest.mark.parametrize(
"series",
[
Series([0, 1]),
Series(date_range("2012-01-01", periods=2)),
Series(date_range("2012-01-01", periods=2, tz="CET")),
],
)
def test_getitem_ndim_deprecated(series):
with tm.assert_produces_warning(
FutureWarning, match="Support for multi-dimensional indexing"
):
result = series[:, None]

assert isinstance(result, np.ndarray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary but nbd

tm.assert_numpy_array_equal(result, np.asarray(series)[:, None])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really pref the
result=
expected=
assert_*

as its what we always want to model



def test_getitem_multilevel_scalar_slice_not_implemented(
Expand Down