Skip to content

Commit 98e495a

Browse files
authored
ENH: Set index of returned dataframe in DatetimeIndex.isocalendar (#34392)
1 parent 707640c commit 98e495a

File tree

7 files changed

+20
-15
lines changed

7 files changed

+20
-15
lines changed

doc/source/user_guide/timeseries.rst

+1
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ You may obtain the year, week and day components of the ISO year from the ISO 86
793793
.. ipython:: python
794794
795795
idx = pd.date_range(start='2019-12-29', freq='D', periods=4)
796+
idx.isocalendar()
796797
idx.to_series().dt.isocalendar()
797798
798799
.. _timeseries.offsets:

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Other enhancements
227227
- :class:`Series.str` now has a `fullmatch` method that matches a regular expression against the entire string in each row of the series, similar to `re.fullmatch` (:issue:`32806`).
228228
- :meth:`DataFrame.sample` will now also allow array-like and BitGenerator objects to be passed to ``random_state`` as seeds (:issue:`32503`)
229229
- :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`)
230-
- :class:`Series.dt` and :class:`DatatimeIndex` now have an `isocalendar` method that returns a :class:`DataFrame` with year, week, and day calculated according to the ISO 8601 calendar (:issue:`33206`).
230+
- :class:`Series.dt` and :class:`DatatimeIndex` now have an `isocalendar` method that returns a :class:`DataFrame` with year, week, and day calculated according to the ISO 8601 calendar (:issue:`33206`, :issue:`34392`).
231231
- The :meth:`DataFrame.to_feather` method now supports additional keyword
232232
arguments (e.g. to set the compression) that are added in pyarrow 0.17
233233
(:issue:`33422`).

pandas/core/arrays/datetimes.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1272,17 +1272,17 @@ def isocalendar(self):
12721272
--------
12731273
>>> idx = pd.date_range(start='2019-12-29', freq='D', periods=4)
12741274
>>> idx.isocalendar()
1275-
year week day
1276-
0 2019 52 7
1277-
1 2020 1 1
1278-
2 2020 1 2
1279-
3 2020 1 3
1275+
year week day
1276+
2019-12-29 2019 52 7
1277+
2019-12-30 2020 1 1
1278+
2019-12-31 2020 1 2
1279+
2020-01-01 2020 1 3
12801280
>>> idx.isocalendar().week
1281-
0 52
1282-
1 1
1283-
2 1
1284-
3 1
1285-
Name: week, dtype: UInt32
1281+
2019-12-29 52
1282+
2019-12-30 1
1283+
2019-12-31 1
1284+
2020-01-01 1
1285+
Freq: D, Name: week, dtype: UInt32
12861286
"""
12871287
from pandas import DataFrame
12881288

pandas/core/indexes/datetimes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _new_DatetimeIndex(cls, d):
6666

6767

6868
@inherit_names(
69-
["to_period", "to_perioddelta", "to_julian_date", "strftime"]
69+
["to_period", "to_perioddelta", "to_julian_date", "strftime", "isocalendar"]
7070
+ DatetimeArray._field_ops
7171
+ DatetimeArray._datetimelike_methods,
7272
DatetimeArray,
@@ -90,7 +90,6 @@ def _new_DatetimeIndex(cls, d):
9090
"date",
9191
"time",
9292
"timetz",
93-
"isocalendar",
9493
]
9594
+ DatetimeArray._bool_ops,
9695
DatetimeArray,

pandas/core/indexes/extension.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pandas.util._decorators import cache_readonly, doc
1111

1212
from pandas.core.dtypes.common import is_dtype_equal, is_object_dtype
13-
from pandas.core.dtypes.generic import ABCSeries
13+
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
1414

1515
from pandas.core.arrays import ExtensionArray
1616
from pandas.core.indexers import deprecate_ndim_indexing
@@ -55,6 +55,8 @@ def fget(self):
5555
if wrap:
5656
if isinstance(result, type(self._data)):
5757
return type(self)._simple_new(result, name=self.name)
58+
elif isinstance(result, ABCDataFrame):
59+
return result.set_index(self)
5860
return Index(result, name=self.name)
5961
return result
6062

@@ -77,6 +79,8 @@ def method(self, *args, **kwargs):
7779
if wrap:
7880
if isinstance(result, type(self._data)):
7981
return type(self)._simple_new(result, name=self.name)
82+
elif isinstance(result, ABCDataFrame):
83+
return result.set_index(self)
8084
return Index(result, name=self.name)
8185
return result
8286

pandas/tests/groupby/transform/test_transform.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ def test_groupby_transform_with_datetimes(func, values):
10221022
dates = pd.date_range("1/1/2011", periods=10, freq="D")
10231023

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

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

pandas/tests/indexes/datetimes/test_misc.py

+1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
404404
expected_data_frame = pd.DataFrame(
405405
[[2013, 52, 7], [2014, 1, 1], [2014, 1, 2]],
406406
columns=["year", "week", "day"],
407+
index=dates,
407408
dtype="UInt32",
408409
)
409410
tm.assert_frame_equal(result, expected_data_frame)

0 commit comments

Comments
 (0)