Skip to content

Commit 591f47f

Browse files
committed
Use self.name to construct the result of DatetimeIndex.snap, fix pandas-dev#25575.
Signed-off-by: HE, Tao <[email protected]>
1 parent 74a9ae3 commit 591f47f

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

doc/source/whatsnew/v0.24.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Fixed Regressions
3131
- Fixed regression in ``IntervalDtype`` construction where passing an incorrect string with 'Interval' as a prefix could result in a ``RecursionError``. (:issue:`25338`)
3232
- Fixed regression in :class:`Categorical`, where constructing it from a categorical ``Series`` and an explicit ``categories=`` that differed from that in the ``Series`` created an invalid object which could trigger segfaults. (:issue:`25318`)
3333
- Fixed pip installing from source into an environment without NumPy (:issue:`25193`)
34+
- Fixed name preserving bug of :meth:`DatetimeIndex.snap` (:issue:`25575`)
3435

3536
.. _whatsnew_0242.enhancements:
3637

pandas/core/indexes/datetimes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,7 @@ def snap(self, freq='S'):
787787
snapped[i] = s
788788

789789
# we know it conforms; skip check
790-
return DatetimeIndex._simple_new(snapped, freq=freq)
791-
# TODO: what about self.name? tz? if so, use shallow_copy?
790+
return DatetimeIndex._simple_new(snapped, name=self.name, freq=freq)
792791

793792
def join(self, other, how='left', level=None, return_indexers=False,
794793
sort=False):

pandas/tests/series/indexing/test_datetime.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ def test_fancy_setitem():
5454

5555
def test_dti_snap():
5656
dti = DatetimeIndex(['1/1/2002', '1/2/2002', '1/3/2002', '1/4/2002',
57-
'1/5/2002', '1/6/2002', '1/7/2002'], freq='D')
57+
'1/5/2002', '1/6/2002', '1/7/2002'],
58+
name='my_dti', freq='D')
5859

5960
res = dti.snap(freq='W-MON')
60-
exp = date_range('12/31/2001', '1/7/2002', freq='w-mon')
61+
exp = date_range('12/31/2001', '1/7/2002', name='my_dti', freq='w-mon')
6162
exp = exp.repeat([3, 4])
6263
assert (res == exp).all()
64+
assert res.name == exp.name
6365

6466
res = dti.snap(freq='B')
6567

0 commit comments

Comments
 (0)