From c719bdf51c02d1ce448e3b36c7597d3bebd96c9f Mon Sep 17 00:00:00 2001 From: Hannah Date: Sat, 1 Sep 2018 10:49:00 +0200 Subject: [PATCH 1/3] BUG: DataFrame.apply not adding a frequency if freq=None (#22150) --- pandas/core/indexes/datetimes.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 019aad4941d26..2e5ccf78ef16c 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -854,8 +854,6 @@ def union_many(self, others): if isinstance(this, DatetimeIndex): this._tz = timezones.tz_standardize(tz) - if this.freq is None: - this.freq = to_offset(this.inferred_freq) return this def join(self, other, how='left', level=None, return_indexers=False, From 827178efd7364421b03f6cc74c7d30e222e5147d Mon Sep 17 00:00:00 2001 From: Hannah Date: Sun, 9 Sep 2018 19:16:34 +0200 Subject: [PATCH 2/3] Added whatsnew and tests --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/tests/frame/test_apply.py | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 7ed92935a0991..cf1ed83df7a6a 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -585,6 +585,7 @@ Datetimelike - Bug in :class:`DataFrame` with mixed dtypes including ``datetime64[ns]`` incorrectly raising ``TypeError`` on equality comparisons (:issue:`13128`,:issue:`22163`) - Bug in :meth:`DataFrame.eq` comparison against ``NaT`` incorrectly returning ``True`` or ``NaN`` (:issue:`15697`,:issue:`22163`) - Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise `OverflowError` (:issue:`22492`, :issue:`22508`) +- Bug in :class:`DatetimeIndex` where frequency was being set if original frequency was 'None' (:issue:`22150`) Timedelta ^^^^^^^^^ diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 8beab3fb816df..6d5f31fe3de1f 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -11,6 +11,8 @@ import warnings import numpy as np +from hypothesis import given +from hypothesis.strategies import composite, dates, integers, sampled_from from pandas import (notna, DataFrame, Series, MultiIndex, date_range, Timestamp, compat) @@ -1155,3 +1157,34 @@ def test_agg_cython_table_raises(self, df, func, expected, axis): # GH21224 with pytest.raises(expected): df.agg(func, axis=axis) + + @composite + def indices(draw, max_length=5): + date = draw( + dates( + min_value=Timestamp.min.ceil("D").to_pydatetime().date(), + max_value=Timestamp.max.floor("D").to_pydatetime().date(), + ).map(Timestamp) + ) + periods = draw(integers(0, max_length)) + freq = draw(sampled_from(list("BDHTS"))) + dr = date_range(date, periods=periods, freq=freq) + return pd.DatetimeIndex(list(dr)) + + @given(index=indices(5), num_columns=integers(0, 5)) + def test_frequency_is_original(self, index, num_columns): + # GH22150 + original = index.copy() + df = DataFrame(True, index=index, columns=range(num_columns)) + df.apply(lambda x: x) + assert index.freq == original.freq + + def test_frequency_is_original_example(self): + # GH22150 + index = pd.DatetimeIndex(['2000-01-03', '2000-01-04', '2000-01-05'], + dtype='datetime64[ns]') + num_columns = 2 + original = index.copy() + df = DataFrame(True, index=index, columns=range(num_columns)) + df.apply(lambda x: x) + assert index.freq == original.freq From 534e739cff675eb19ed318615fd4b61164848cde Mon Sep 17 00:00:00 2001 From: Hannah Date: Sun, 16 Sep 2018 13:30:45 +0200 Subject: [PATCH 3/3] Removed redundant test test_frequency_is_original_example Fixed syntax in whatsnew --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/tests/frame/test_apply.py | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 50d3c9799465d..fe74258325902 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -635,7 +635,7 @@ Datetimelike - Bug in :meth:`DataFrame.eq` comparison against ``NaT`` incorrectly returning ``True`` or ``NaN`` (:issue:`15697`, :issue:`22163`) - Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise ``OverflowError`` (:issue:`22492`, :issue:`22508`) - Bug in :class:`DatetimeIndex` incorrectly allowing indexing with ``Timedelta`` object (:issue:`20464`) -- Bug in :class:`DatetimeIndex` where frequency was being set if original frequency was 'None' (:issue:`22150`) +- Bug in :class:`DatetimeIndex` where frequency was being set if original frequency was ``None`` (:issue:`22150`) Timedelta ^^^^^^^^^ diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 6d5f31fe3de1f..1452e1ab8d98d 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -1178,13 +1178,3 @@ def test_frequency_is_original(self, index, num_columns): df = DataFrame(True, index=index, columns=range(num_columns)) df.apply(lambda x: x) assert index.freq == original.freq - - def test_frequency_is_original_example(self): - # GH22150 - index = pd.DatetimeIndex(['2000-01-03', '2000-01-04', '2000-01-05'], - dtype='datetime64[ns]') - num_columns = 2 - original = index.copy() - df = DataFrame(True, index=index, columns=range(num_columns)) - df.apply(lambda x: x) - assert index.freq == original.freq