Skip to content

BUG: to_period() freq was not infered #33406

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

Merged
merged 15 commits into from
Apr 12, 2020
Merged
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ Datetimelike
- Bug in :meth:`DatetimeIndex.searchsorted` not accepting a ``list`` or :class:`Series` as its argument (:issue:`32762`)
- Bug where :meth:`PeriodIndex` raised when passed a :class:`Series` of strings (:issue:`26109`)
- Bug in :class:`Timestamp` arithmetic when adding or subtracting a ``np.ndarray`` with ``timedelta64`` dtype (:issue:`33296`)
- Bug in :meth:`DatetimeIndex.to_period` not infering the frequency when called with no arguments (:issue:`33358`)


Timedelta
^^^^^^^^^
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
timezones,
tzconversion,
)
import pandas._libs.tslibs.frequencies as libfrequencies
from pandas.errors import PerformanceWarning

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -1097,7 +1098,14 @@ def to_period(self, freq=None):
"You must pass a freq argument as current index has none."
)

freq = get_period_alias(freq)
res = get_period_alias(freq)

# https://github.com/pandas-dev/pandas/issues/33358
if res is None:
base, stride = libfrequencies._base_and_stride(freq)
res = f"{stride}{base}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MomIsBestFriend why cant we just do res = freq here? (im trying to clean up usages of base_and_stride)


freq = res

return PeriodArray._from_datetime64(self._data, freq, tz=self.tz)

Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/indexes/datetimes/test_to_period.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import dateutil.tz
from dateutil.tz import tzlocal
import pytest
Expand Down Expand Up @@ -75,6 +77,28 @@ def test_to_period_monthish(self):
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
date_range("01-Jan-2012", periods=8, freq="EOM")

def test_to_period_infer(self):
# https://github.com/pandas-dev/pandas/issues/33358
rng = date_range(
start="2019-12-22 06:40:00+00:00",
end="2019-12-22 08:45:00+00:00",
freq="5min",
)

with tm.assert_produces_warning(None):
# Using simple filter because we are not checking for the warning here
warnings.simplefilter("ignore", UserWarning)

pi1 = rng.to_period("5min")

with tm.assert_produces_warning(None):
# Using simple filter because we are not checking for the warning here
warnings.simplefilter("ignore", UserWarning)

pi2 = rng.to_period()

tm.assert_index_equal(pi1, pi2)

def test_period_dt64_round_trip(self):
dti = date_range("1/1/2000", "1/7/2002", freq="B")
pi = dti.to_period()
Expand Down