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 4 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
9 changes: 8 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,13 @@ 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)

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
18 changes: 18 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,22 @@ 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",
)

# Using simple filter because we are not checking for the warning here
warnings.simplefilter("ignore", UserWarning)
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the warning from? this would not be set like this
use assert_produces_warning(None):
warnings.simplefiler....

if you really need to

Copy link
Member Author

Choose a reason for hiding this comment

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

what is the warning from? this would not be set like this

The warning raises when a user is attempting to convert a date_range to period, if the date_range have a timezone it will drop it in the conversion, the warning let the user know about that:

if self.tz is not None:
warnings.warn(
"Converting to PeriodArray/Index representation "
"will drop timezone information.",
UserWarning,
)

use assert_produces_warning(None):
warnings.simplefiler....

if you really need to

Sure! will fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

i c, ok then


pi1 = rng.to_period("5min")
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