Skip to content

Commit

Permalink
BUG: Preserve name in DatetimeIndex.snap (#25585)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighingnow authored and jreback committed Mar 13, 2019
1 parent 86879ac commit 203481f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Other API Changes
- :class:`DatetimeTZDtype` will now standardize pytz timezones to a common timezone instance (:issue:`24713`)
- ``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`)
- :meth:`Timestamp.strptime` will now rise a ``NotImplementedError`` (:issue:`25016`)
-
- Bug in :meth:`DatetimeIndex.snap` which didn't preserving the ``name`` of the input :class:`Index` (:issue:`25575`)

.. _whatsnew_0250.deprecations:

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ def snap(self, freq='S'):
snapped[i] = s

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

def join(self, other, how='left', level=None, return_indexers=False,
sort=False):
Expand Down
28 changes: 18 additions & 10 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,28 @@ def test_fancy_setitem():
assert (s[48:54] == -3).all()


def test_dti_snap():
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.parametrize('tz', [None, 'Asia/Shanghai', 'Europe/Berlin'])
@pytest.mark.parametrize('name', [None, 'my_dti'])
def test_dti_snap(name, tz):
dti = DatetimeIndex(['1/1/2002', '1/2/2002', '1/3/2002', '1/4/2002',
'1/5/2002', '1/6/2002', '1/7/2002'], freq='D')
'1/5/2002', '1/6/2002', '1/7/2002'],
name=name, tz=tz, freq='D')

res = dti.snap(freq='W-MON')
exp = date_range('12/31/2001', '1/7/2002', freq='w-mon')
exp = exp.repeat([3, 4])
assert (res == exp).all()
result = dti.snap(freq='W-MON')
expected = date_range('12/31/2001', '1/7/2002',
name=name, tz=tz, freq='w-mon')
expected = expected.repeat([3, 4])
tm.assert_index_equal(result, expected)
assert result.tz == expected.tz

res = dti.snap(freq='B')
result = dti.snap(freq='B')

exp = date_range('1/1/2002', '1/7/2002', freq='b')
exp = exp.repeat([1, 1, 1, 2, 2])
assert (res == exp).all()
expected = date_range('1/1/2002', '1/7/2002',
name=name, tz=tz, freq='b')
expected = expected.repeat([1, 1, 1, 2, 2])
tm.assert_index_equal(result, expected)
assert result.tz == expected.tz


def test_dti_reset_index_round_trip():
Expand Down

0 comments on commit 203481f

Please sign in to comment.