Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions doc/source/user_guide/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ You may obtain the year, week and day components of the ISO year from the ISO 86
.. ipython:: python

idx = pd.date_range(start='2019-12-29', freq='D', periods=4)
idx.isocalendar()
idx.to_series().dt.isocalendar()

.. _timeseries.offsets:
Expand Down
20 changes: 10 additions & 10 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,17 +1272,17 @@ def isocalendar(self):
--------
>>> idx = pd.date_range(start='2019-12-29', freq='D', periods=4)
>>> idx.isocalendar()
year week day
0 2019 52 7
1 2020 1 1
2 2020 1 2
3 2020 1 3
year week day
2019-12-29 2019 52 7
2019-12-30 2020 1 1
2019-12-31 2020 1 2
2020-01-01 2020 1 3
>>> idx.isocalendar().week
0 52
1 1
2 1
3 1
Name: week, dtype: UInt32
2019-12-29 52
2019-12-30 1
2019-12-31 1
2020-01-01 1
Freq: D, Name: week, dtype: UInt32
"""
from pandas import DataFrame

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _new_DatetimeIndex(cls, d):


@inherit_names(
["to_period", "to_perioddelta", "to_julian_date", "strftime"]
["to_period", "to_perioddelta", "to_julian_date", "strftime", "isocalendar"]
+ DatetimeArray._field_ops
+ DatetimeArray._datetimelike_methods,
DatetimeArray,
Expand All @@ -90,7 +90,6 @@ def _new_DatetimeIndex(cls, d):
"date",
"time",
"timetz",
"isocalendar",
]
+ DatetimeArray._bool_ops,
DatetimeArray,
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pandas.util._decorators import cache_readonly, doc

from pandas.core.dtypes.common import is_dtype_equal, is_object_dtype
from pandas.core.dtypes.generic import ABCSeries
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries

from pandas.core.arrays import ExtensionArray
from pandas.core.indexers import deprecate_ndim_indexing
Expand Down Expand Up @@ -55,6 +55,8 @@ def fget(self):
if wrap:
if isinstance(result, type(self._data)):
return type(self)._simple_new(result, name=self.name)
elif isinstance(result, ABCDataFrame):
return result.set_index(self)
return Index(result, name=self.name)
return result

Expand All @@ -77,6 +79,8 @@ def method(self, *args, **kwargs):
if wrap:
if isinstance(result, type(self._data)):
return type(self)._simple_new(result, name=self.name)
elif isinstance(result, ABCDataFrame):
return result.set_index(self)
return Index(result, name=self.name)
return result

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ def test_groupby_transform_with_datetimes(func, values):
dates = pd.date_range("1/1/2011", periods=10, freq="D")

stocks = pd.DataFrame({"price": np.arange(10.0)}, index=dates)
stocks["week_id"] = dates.isocalendar().set_index(dates).week
stocks["week_id"] = dates.isocalendar().week

result = stocks.groupby(stocks["week_id"])["price"].transform(func)

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
expected_data_frame = pd.DataFrame(
[[2013, 52, 7], [2014, 1, 1], [2014, 1, 2]],
columns=["year", "week", "day"],
index=dates,
dtype="UInt32",
)
tm.assert_frame_equal(result, expected_data_frame)