Skip to content

Commit 58b1732

Browse files
jbrockmendeljreback
authored andcommitted
BUG: Fix indexing on DatetimeBlock (#27110)
1 parent 02b552d commit 58b1732

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

doc/source/whatsnew/v0.25.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ Indexing
774774
- Fixed bug where assigning a :class:`arrays.PandasArray` to a :class:`pandas.core.frame.DataFrame` would raise error (:issue:`26390`)
775775
- Allow keyword arguments for callable local reference used in the :meth:`DataFrame.query` string (:issue:`26426`)
776776
- Bug which produced ``AttributeError`` on partial matching :class:`Timestamp` in a :class:`MultiIndex` (:issue:`26944`)
777+
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` on a :class:`DataFrame` with a single timezone-aware datetime64[ns] column incorrectly returning a scalar instead of a :class:`Series` (:issue:`27110`)
778+
-
777779

778780
Missing
779781
^^^^^^^

pandas/core/internals/blocks.py

+4
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,10 @@ def iget(self, col):
15381538
col, loc = col
15391539
if not com.is_null_slice(col) and col != 0:
15401540
raise IndexError("{0} only contains one item".format(self))
1541+
elif isinstance(col, slice):
1542+
if col != slice(None):
1543+
raise NotImplementedError(col)
1544+
return self.values[[loc]]
15411545
return self.values[loc]
15421546
else:
15431547
if col != 0:

pandas/tests/extension/base/getitem.py

+14
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ def test_loc_frame(self, data):
7373
result = df.loc[:3, 'A']
7474
self.assert_series_equal(result, expected)
7575

76+
def test_loc_iloc_frame_single_dtype(self, data):
77+
# GH#27110 bug in ExtensionBlock.iget caused df.iloc[n] to incorrectly
78+
# return a scalar
79+
df = pd.DataFrame({"A": data})
80+
expected = pd.Series([data[2]], index=["A"], name=2, dtype=data.dtype)
81+
82+
result = df.loc[2]
83+
self.assert_series_equal(result, expected)
84+
85+
expected = pd.Series([data[-1]], index=["A"], name=len(data) - 1,
86+
dtype=data.dtype)
87+
result = df.iloc[-1]
88+
self.assert_series_equal(result, expected)
89+
7690
def test_getitem_scalar(self, data):
7791
result = data[0]
7892
assert isinstance(result, data.dtype.type)

pandas/tests/extension/test_numpy.py

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ def test_take_series(self, data):
180180
# ValueError: PandasArray must be 1-dimensional.
181181
super().test_take_series(data)
182182

183+
@pytest.mark.xfail(reason="astype doesn't recognize data.dtype")
184+
def test_loc_iloc_frame_single_dtype(self, data):
185+
super().test_loc_iloc_frame_single_dtype(data)
186+
183187

184188
class TestGroupby(BaseNumPyTests, base.BaseGroupbyTests):
185189
@skip_nested

pandas/tests/groupby/aggregate/test_other.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,15 @@ def test_agg_timezone_round_trip():
421421
assert ts == grouped.nth(0)['B'].iloc[0]
422422
assert ts == grouped.head(1)['B'].iloc[0]
423423
assert ts == grouped.first()['B'].iloc[0]
424-
assert ts == grouped.apply(lambda x: x.iloc[0])[0]
424+
425+
# GH#27110 applying iloc should return a DataFrame
426+
assert ts == grouped.apply(lambda x: x.iloc[0]).iloc[0, 0]
425427

426428
ts = df['B'].iloc[2]
427429
assert ts == grouped.last()['B'].iloc[0]
428-
assert ts == grouped.apply(lambda x: x.iloc[-1])[0]
430+
431+
# GH#27110 applying iloc should return a DataFrame
432+
assert ts == grouped.apply(lambda x: x.iloc[-1]).iloc[0, 0]
429433

430434

431435
def test_sum_uint64_overflow():

pandas/tests/indexing/test_datetime.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_setitem_with_datetime_tz(self):
3939

4040
def test_indexing_with_datetime_tz(self):
4141

42-
# 8260
42+
# GH#8260
4343
# support datetime64 with tz
4444

4545
idx = Index(date_range('20130101', periods=3, tz='US/Eastern'),
@@ -65,11 +65,12 @@ def test_indexing_with_datetime_tz(self):
6565
# indexing - fast_xs
6666
df = DataFrame({'a': date_range('2014-01-01', periods=10, tz='UTC')})
6767
result = df.iloc[5]
68-
expected = Timestamp('2014-01-06 00:00:00+0000', tz='UTC', freq='D')
69-
assert result == expected
68+
expected = Series([Timestamp('2014-01-06 00:00:00+0000', tz='UTC')],
69+
index=['a'], name=5)
70+
tm.assert_series_equal(result, expected)
7071

7172
result = df.loc[5]
72-
assert result == expected
73+
tm.assert_series_equal(result, expected)
7374

7475
# indexing - boolean
7576
result = df[df.a > df.a[3]]

0 commit comments

Comments
 (0)