8000 BUG: Preserve name in DatetimeIndex.snap by sighingnow · Pull Request #25585 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: Preserve name in DatetimeIndex.snap #25585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move to 0.25.0, handle tz correctly and revise test case.
Signed-off-by: HE, Tao <sighingnow@gmail.com>
  • Loading branch information
sighingnow committed Mar 12, 2019
commit 457d0b6b04ba9c21d71a326291ed80048e616fe7
1 change: 0 additions & 1 deletion doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Fixed Regressions
- Fixed regression in ``IntervalDtype`` construction where passing an incorrect string with 'Interval' as a prefix could result in a ``RecursionError``. (:issue:`25338`)
- 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`)
- Fixed pip installing from source into an environment without NumPy (:issue:`25193`)
- Fixed name preserving bug of :meth:`DatetimeIndex.snap` (:issue:`25575`)

.. _whatsnew_0242.enhancements:

Expand Down
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
3 changes: 2 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,8 @@ def snap(self, freq='S'):
snapped[i] = s

# we know it conforms; skip check
return DatetimeIndex._simple_new(snapped, name=self.name, freq=freq)
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
23 changes: 13 additions & 10 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,22 @@ def test_fancy_setitem():
def test_dti_snap():
dti = DatetimeIndex(['1/1/2002', '1/2/2002', '1/3/2002', '1/4/2002',
'1/5/2002', '1/6/2002', '1/7/2002'],
name='my_dti', freq='D')
name='my_dti', tz='Asia/Shanghai', freq='D')

res = dti.snap(freq='W-MON')
exp = date_range('12/31/2001', '1/7/2002', name='my_dti', freq='w-mon')
exp = exp.repeat([3, 4])
assert (res == exp).all()
assert res.name == exp.name
result = dti.snap(freq='W-MON')
expected = date_range('12/31/2001', '1/7/2002', name='my_dti',
tz='Asia/Shanghai', 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='my_dti',
tz='Asia/Shanghai', 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