File tree 3 files changed +35
-1
lines changed
3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -410,6 +410,8 @@ Datetimelike
410
410
- Bug in :meth: `DatetimeIndex.searchsorted ` not accepting a ``list `` or :class: `Series ` as its argument (:issue: `32762 `)
411
411
- Bug where :meth: `PeriodIndex ` raised when passed a :class: `Series ` of strings (:issue: `26109 `)
412
412
- Bug in :class: `Timestamp ` arithmetic when adding or subtracting a ``np.ndarray `` with ``timedelta64 `` dtype (:issue: `33296 `)
413
+ - Bug in :meth: `DatetimeIndex.to_period ` not infering the frequency when called with no arguments (:issue: `33358 `)
414
+
413
415
414
416
Timedelta
415
417
^^^^^^^^^
Original file line number Diff line number Diff line change 18
18
timezones ,
19
19
tzconversion ,
20
20
)
21
+ import pandas ._libs .tslibs .frequencies as libfrequencies
21
22
from pandas .errors import PerformanceWarning
22
23
23
24
from pandas .core .dtypes .common import (
@@ -1097,7 +1098,14 @@ def to_period(self, freq=None):
1097
1098
"You must pass a freq argument as current index has none."
1098
1099
)
1099
1100
1100
- freq = get_period_alias (freq )
1101
+ res = get_period_alias (freq )
1102
+
1103
+ # https://github.com/pandas-dev/pandas/issues/33358
1104
+ if res is None :
1105
+ base , stride = libfrequencies ._base_and_stride (freq )
1106
+ res = f"{ stride } { base } "
1107
+
1108
+ freq = res
1101
1109
1102
1110
return PeriodArray ._from_datetime64 (self ._data , freq , tz = self .tz )
1103
1111
Original file line number Diff line number Diff line change
1
+ import warnings
2
+
1
3
import dateutil .tz
2
4
from dateutil .tz import tzlocal
3
5
import pytest
@@ -75,6 +77,28 @@ def test_to_period_monthish(self):
75
77
with pytest .raises (ValueError , match = INVALID_FREQ_ERR_MSG ):
76
78
date_range ("01-Jan-2012" , periods = 8 , freq = "EOM" )
77
79
80
+ def test_to_period_infer (self ):
81
+ # https://github.com/pandas-dev/pandas/issues/33358
82
+ rng = date_range (
83
+ start = "2019-12-22 06:40:00+00:00" ,
84
+ end = "2019-12-22 08:45:00+00:00" ,
85
+ freq = "5min" ,
86
+ )
87
+
88
+ with tm .assert_produces_warning (None ):
89
+ # Using simple filter because we are not checking for the warning here
90
+ warnings .simplefilter ("ignore" , UserWarning )
91
+
92
+ pi1 = rng .to_period ("5min" )
93
+
94
+ with tm .assert_produces_warning (None ):
95
+ # Using simple filter because we are not checking for the warning here
96
+ warnings .simplefilter ("ignore" , UserWarning )
97
+
98
+ pi2 = rng .to_period ()
99
+
100
+ tm .assert_index_equal (pi1 , pi2 )
101
+
78
102
def test_period_dt64_round_trip (self ):
79
103
dti = date_range ("1/1/2000" , "1/7/2002" , freq = "B" )
80
104
pi = dti .to_period ()
You can’t perform that action at this time.
0 commit comments