Skip to content

Commit 48d76d6

Browse files
mroeschkejreback
authored andcommitted
BUG: pd.concat does not drop DatetimeIndex.freq when result is monotonic with even spacing (#25796)
1 parent d37c531 commit 48d76d6

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Reshaping
312312
- Bug in :func:`merge` when merging by index name would sometimes result in an incorrectly numbered index (:issue:`24212`)
313313
- :func:`to_records` now accepts dtypes to its `column_dtypes` parameter (:issue:`24895`)
314314
- Bug in :func:`concat` where order of ``OrderedDict`` (and ``dict`` in Python 3.6+) is not respected, when passed in as ``objs`` argument (:issue:`21510`)
315-
315+
- Bug in :func:`concat` where the resulting ``freq`` of two :class:`DatetimeIndex` with the same ``freq`` would be dropped (:issue:`3232`).
316316

317317
Sparse
318318
^^^^^^

pandas/core/indexes/datetimelike.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99

1010
from pandas._libs import NaT, iNaT, lib
11+
from pandas._libs.algos import unique_deltas
1112
from pandas.compat.numpy import function as nv
1213
from pandas.errors import AbstractMethodError
1314
from pandas.util._decorators import Appender, cache_readonly, deprecate_kwarg
@@ -587,11 +588,15 @@ def _concat_same_dtype(self, to_concat, name):
587588
if len({str(x.dtype) for x in to_concat}) != 1:
588589
raise ValueError('to_concat must have the same tz')
589590

590-
if not is_period_dtype(self):
591+
new_data = type(self._values)._concat_same_type(to_concat).asi8
592+
593+
# GH 3232: If the concat result is evenly spaced, we can retain the
594+
# original frequency
595+
is_diff_evenly_spaced = len(unique_deltas(new_data)) == 1
596+
if not is_period_dtype(self) and not is_diff_evenly_spaced:
591597
# reset freq
592598
attribs['freq'] = None
593599

594-
new_data = type(self._values)._concat_same_type(to_concat).asi8
595600
return self._simple_new(new_data, **attribs)
596601

597602
@Appender(_index_shared_docs['astype'])

pandas/tests/reshape/test_concat.py

+17
Original file line numberDiff line numberDiff line change
@@ -2532,3 +2532,20 @@ def test_concat_categorical_tz():
25322532
'a', 'b'
25332533
])
25342534
tm.assert_series_equal(result, expected)
2535+
2536+
2537+
def test_concat_datetimeindex_freq():
2538+
# GH 3232
2539+
# Monotonic index result
2540+
dr = pd.date_range('01-Jan-2013', periods=100, freq='50L', tz='UTC')
2541+
data = list(range(100))
2542+
expected = pd.DataFrame(data, index=dr)
2543+
result = pd.concat([expected[:50], expected[50:]])
2544+
tm.assert_frame_equal(result, expected)
2545+
2546+
# Non-monotonic index result
2547+
result = pd.concat([expected[50:], expected[:50]])
2548+
expected = pd.DataFrame(data[50:] + data[:50],
2549+
index=dr[50:].append(dr[:50]))
2550+
expected.index.freq = None
2551+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)