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: added fix for column index for DatetimeIndex type #60528

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4002,6 +4002,13 @@ class max_speed
indices = np.asarray(indices, dtype=np.intp)
if axis == 0 and indices.ndim == 1 and is_range_indexer(indices, len(self)):
return self.copy(deep=False)


if axis == 1 and indices.ndim == 1 and is_range_indexer(indices, len(self.columns)):
# check if index is of type period, sets to object for slicing correct columns
if isinstance(self.index, DatetimeIndex) or isinstance(self.index, PeriodIndex):
return self.copy(deep=False).astype('object')
return self.copy(deep=False)

new_data = self._mgr.take(
indices,
Expand Down
29 changes: 28 additions & 1 deletion pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Categorical,
DataFrame,
DatetimeIndex,
PeriodIndex,
Index,
Series,
Timestamp,
Expand Down Expand Up @@ -411,7 +412,33 @@ def test_getitem_uint_array_key(self, any_unsigned_int_numpy_dtype):
ser[key]
with pytest.raises(KeyError, match="4"):
ser.loc[key]



def test_pivot_periodindex_hourly(self):
#GH issue 60273
pr = period_range('2024-01-01 00:00:00', '2024-01-01 02:00:00', freq='h')
df = DataFrame(index=pr)
df['date'] = df.index.to_timestamp().floor('D')
df['hour'] = df.index.hour
df.index.name = 'value'
df = df.reset_index()
df = df.pivot(index='date', columns='hour', values='value')
df=df.astype('object')
result = df[[0, 1, 2]]
expected = df

tm.assert_frame_equal(result, expected)

def test_getitem_periodindex_daily(self):
# GH issue 60273
df = pd.DataFrame(
data=[
pd.Period("2024-01-01", freq="D"),
pd.Period("2024-01-02", freq="D"),
pd.Period("2024-01-03", freq="D"),
]
).T
tm.assert_frame_equal(df[[0, 1, 2]], df)

class TestGetitemBooleanMask:
def test_getitem_boolean(self, string_series):
Expand Down
Loading