From 5cb07c8864ff98b4f2c6f5bc57a5ea586ed19f1d Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 8 Apr 2020 22:57:09 +0300 Subject: [PATCH 1/9] BUG: to_period() freq was not infered --- pandas/core/arrays/datetimes.py | 11 +++++------ .../tests/indexes/datetimes/test_to_period.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index b9f9edcebad5b..2d18a661794c3 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -1090,15 +1090,14 @@ def to_period(self, freq=None): ) if freq is None: - freq = self.freqstr or self.inferred_freq - - if freq is None: - raise ValueError( - "You must pass a freq argument as current index has none." - ) + freq = self.inferred_freq or self.freqstr + if freq is None: freq = get_period_alias(freq) + if freq is None: + raise ValueError("You must pass a freq argument as current index has none.") + return PeriodArray._from_datetime64(self._data, freq, tz=self.tz) def to_perioddelta(self, freq): diff --git a/pandas/tests/indexes/datetimes/test_to_period.py b/pandas/tests/indexes/datetimes/test_to_period.py index 7b75e676a2c12..0f119458a8d46 100644 --- a/pandas/tests/indexes/datetimes/test_to_period.py +++ b/pandas/tests/indexes/datetimes/test_to_period.py @@ -1,3 +1,5 @@ +import warnings + import dateutil.tz from dateutil.tz import tzlocal import pytest @@ -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) + + 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() From 10ac1de9fa6ba6ce8f751aac3d46d5ab6b9bb7f4 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 10 Apr 2020 17:04:42 +0300 Subject: [PATCH 2/9] Better handling of period --- pandas/core/arrays/datetimes.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 2d18a661794c3..7fd68de7c7f3d 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -18,6 +18,7 @@ timezones, tzconversion, ) +import pandas._libs.tslibs.frequencies as libfrequencies from pandas.errors import PerformanceWarning from pandas.core.dtypes.common import ( @@ -1090,13 +1091,20 @@ def to_period(self, freq=None): ) if freq is None: - freq = self.inferred_freq or self.freqstr + freq = self.freqstr or self.inferred_freq - if freq is None: - freq = get_period_alias(freq) + if freq is None: + raise ValueError( + "You must pass a freq argument as current index has none." + ) - if freq is None: - raise ValueError("You must pass a freq argument as current index has none.") + res = get_period_alias(freq) + + if res is None: + base, stride = libfrequencies._base_and_stride(freq) + res = f"{stride}{base}" + + freq = res return PeriodArray._from_datetime64(self._data, freq, tz=self.tz) From 4dcd4e3cbdb5b05ba9691032e1dc27778e88ebdb Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 10 Apr 2020 20:52:55 +0300 Subject: [PATCH 3/9] Using tm.assert_produces_warning xref: https://github.com/pandas-dev/pandas/pull/33406#discussion_r406859019 --- pandas/tests/indexes/datetimes/test_to_period.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_to_period.py b/pandas/tests/indexes/datetimes/test_to_period.py index 0f119458a8d46..c9d36de373edd 100644 --- a/pandas/tests/indexes/datetimes/test_to_period.py +++ b/pandas/tests/indexes/datetimes/test_to_period.py @@ -85,11 +85,17 @@ def test_to_period_infer(self): freq="5min", ) - # Using simple filter because we are not checking for the warning here - warnings.simplefilter("ignore", UserWarning) + 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") - pi2 = rng.to_period() + pi1 = rng.to_period("5min") + + 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) From e5973a8a83a2f19773f35e429e136a6ce95d220a Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 10 Apr 2020 20:54:37 +0300 Subject: [PATCH 4/9] Added `with` contex manager --- pandas/tests/indexes/datetimes/test_to_period.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_to_period.py b/pandas/tests/indexes/datetimes/test_to_period.py index c9d36de373edd..d82fc1ef6743b 100644 --- a/pandas/tests/indexes/datetimes/test_to_period.py +++ b/pandas/tests/indexes/datetimes/test_to_period.py @@ -85,13 +85,13 @@ def test_to_period_infer(self): freq="5min", ) - tm.assert_produces_warning(None): + 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") - tm.assert_produces_warning(None): + with tm.assert_produces_warning(None): # Using simple filter because we are not checking for the warning here warnings.simplefilter("ignore", UserWarning) From 1b031a1512298f5ebdcc720e7147ce5cbaa20261 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 10 Apr 2020 20:57:05 +0300 Subject: [PATCH 5/9] Added comment pointing to the original issue --- pandas/core/arrays/datetimes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 7fd68de7c7f3d..77001f274e8e8 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -1100,6 +1100,7 @@ def to_period(self, freq=None): 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}" From 55df3fcf62f29e0d53b8eece67fe0e043f53188f Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 11 Apr 2020 15:43:54 +0300 Subject: [PATCH 6/9] Added whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 718de09a0c3e4..27a18b3b92bfb 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -409,6 +409,7 @@ 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 ^^^^^^^^^ From ea2f5c73b462896152ceb73823cf55c628bee8aa Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 11 Apr 2020 15:56:07 +0300 Subject: [PATCH 7/9] (EMPTY COMMIT) Restarting CI From 5de3611662d0d173bedfca03961ab33186cbd491 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 11 Apr 2020 16:51:48 +0300 Subject: [PATCH 8/9] Added extra blank line --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 27a18b3b92bfb..f3b301a29b8c3 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -411,6 +411,7 @@ Datetimelike - 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 ^^^^^^^^^ From 0274c624cba19440f1be609116d2fd9f65c69e79 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 11 Apr 2020 17:37:20 +0300 Subject: [PATCH 9/9] (EMPTY COMMIT) Restarting CI