Skip to content

Commit 203481f

Browse files
sighingnowjreback
authored andcommitted
BUG: Preserve name in DatetimeIndex.snap (#25585)
1 parent 86879ac commit 203481f

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Other API Changes
8686
- :class:`DatetimeTZDtype` will now standardize pytz timezones to a common timezone instance (:issue:`24713`)
8787
- ``Timestamp`` and ``Timedelta`` scalars now implement the :meth:`to_numpy` method as aliases to :meth:`Timestamp.to_datetime64` and :meth:`Timedelta.to_timedelta64`, respectively. (:issue:`24653`)
8888
- :meth:`Timestamp.strptime` will now rise a ``NotImplementedError`` (:issue:`25016`)
89-
-
89+
- Bug in :meth:`DatetimeIndex.snap` which didn't preserving the ``name`` of the input :class:`Index` (:issue:`25575`)
9090

9191
.. _whatsnew_0250.deprecations:
9292

pandas/core/indexes/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,8 @@ 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, tz=self.tz,
791+
freq=freq)
792792

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

pandas/tests/series/indexing/test_datetime.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,28 @@ def test_fancy_setitem():
5252
assert (s[48:54] == -3).all()
5353

5454

55-
def test_dti_snap():
55+
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
56+
@pytest.mark.parametrize('tz', [None, 'Asia/Shanghai', 'Europe/Berlin'])
57+
@pytest.mark.parametrize('name', [None, 'my_dti'])
58+
def test_dti_snap(name, tz):
5659
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')
60+
'1/5/2002', '1/6/2002', '1/7/2002'],
61+
name=name, tz=tz, freq='D')
5862

59-
res = dti.snap(freq='W-MON')
60-
exp = date_range('12/31/2001', '1/7/2002', freq='w-mon')
61-
exp = exp.repeat([3, 4])
62-
assert (res == exp).all()
63+
result = dti.snap(freq='W-MON')
64+
expected = date_range('12/31/2001', '1/7/2002',
65+
name=name, tz=tz, freq='w-mon')
66+
expected = expected.repeat([3, 4])
67+
tm.assert_index_equal(result, expected)
68+
assert result.tz == expected.tz
6369

64-
res = dti.snap(freq='B')
70+
result = dti.snap(freq='B')
6571

66-
exp = date_range('1/1/2002', '1/7/2002', freq='b')
67-
exp = exp.repeat([1, 1, 1, 2, 2])
68-
assert (res == exp).all()
72+
expected = date_range('1/1/2002', '1/7/2002',
73+
name=name, tz=tz, freq='b')
74+
expected = expected.repeat([1, 1, 1, 2, 2])
75+
tm.assert_index_equal(result, expected)
76+
assert result.tz == expected.tz
6977

7078

7179
def test_dti_reset_index_round_trip():

0 commit comments

Comments
 (0)