Skip to content

Commit dda5439

Browse files
committed
simplify timestamp precision issue
1 parent a2d5ec7 commit dda5439

File tree

4 files changed

+13
-54
lines changed

4 files changed

+13
-54
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,12 @@ Returning a ``Series`` allows one to control the exact return structure and colu
598598
.. _whatsnew_0230.api_breaking.end_time:
599599

600600
Time values in ``dt.end_time`` and ``to_timestamp(how='end')``
601-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
601+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
602602

603-
The time values in ``Period`` and ``PeriodIndex`` objects are now adjusted
604-
appropriately when calling :attr:`Series.dt.end_time`, :attr:`Period.end_time`,
603+
The time values in :class:`Period` and :class:`PeriodIndex` objects are now set
604+
to '23:59:59.999999999' when calling :attr:`Series.dt.end_time`, :attr:`Period.end_time`,
605605
:attr:`PeriodIndex.end_time`, :func:`Period.to_timestamp()` with ``how='end'``,
606-
and :func:`PeriodIndex.to_timestamp()` with ``how='end'`` (:issue:`17157`)
606+
or :func:`PeriodIndex.to_timestamp()` with ``how='end'`` (:issue:`17157`)
607607

608608
Previous Behavior:
609609

@@ -615,22 +615,11 @@ Previous Behavior:
615615
In [4]: pd.Series(p).dt.end_time[0]
616616
Out[4]: Timestamp(2017-01-01 00:00:00)
617617

618-
In [5]: pd.Series(pi).dt.end_time[0]
618+
In [5]: pi.end_time[0]
619619
Out[5]: Timestamp(2017-01-01 00:00:00)
620620

621-
In [6]: p.end_time
622-
Out[6]: Timestamp(2017-01-01 23:59:59.999999999)
623-
624-
In [7]: pi.end_time[0]
625-
Out[7]: Timestamp(2017-01-01 00:00:00)
626-
627-
In [8]: p.to_timestamp(how='end')
628-
Out[8]: Timestamp(2017-01-01 00:00:00)
629-
630-
In [9]: pi.to_timestamp(how='end')[0]
631-
Out[9]: Timestamp(2017-01-01 00:00:00)
632-
633-
Current Behavior
621+
In [6]: pi.to_timestamp(how='end')
622+
Out[6]: Timestamp(2017-01-01 00:00:00)
634623

635624
.. ipython:: python
636625

@@ -639,15 +628,9 @@ Current Behavior
639628

640629
pd.Series(p).dt.end_time[0]
641630

642-
pd.Series(pi).dt.end_time[0]
643-
644-
p.end_time
645-
646631
pi.end_time[0]
647632

648-
p.to_timestamp(how='end')
649-
650-
pi.to_timestamp(how='end')[0]
633+
pi.to_timestamp(how='end')
651634

652635

653636
Build Changes

pandas/core/indexes/datetimes.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import pandas.core.tools.datetimes as tools
5454

5555
from pandas._libs import (lib, index as libindex, tslib as libts,
56-
join as libjoin, Timestamp, Timedelta)
56+
join as libjoin, Timestamp)
5757
from pandas._libs.tslibs import (timezones, conversion, fields, parsing,
5858
resolution as libresolution)
5959

@@ -2545,28 +2545,14 @@ def _generate_regular_range(start, end, periods, offset):
25452545
data = np.arange(b, e, stride, dtype=np.int64)
25462546
data = DatetimeIndex._simple_new(data, None, tz=tz)
25472547
else:
2548-
if isinstance(start, Timestamp):
2549-
start_date = start.to_pydatetime()
2550-
else:
2551-
start_date = start
2552-
2553-
if isinstance(end, Timestamp):
2554-
end_date = end.to_pydatetime()
2555-
else:
2556-
end_date = end
25572548

2558-
xdr = generate_range(start=start_date, end=end_date,
2549+
xdr = generate_range(start=start, end=end,
25592550
periods=periods, offset=offset)
25602551

25612552
dates = list(xdr)
25622553
# utc = len(dates) > 0 and dates[0].tzinfo is not None
25632554
data = tools.to_datetime(dates)
25642555

2565-
# Add back in the lost nanoseconds
2566-
if isinstance(start, Timestamp) and isinstance(end, Timestamp):
2567-
if start.nanosecond == 999 and end.nanosecond == 999:
2568-
data = data + Timedelta(999, 'ns')
2569-
25702556
return data
25712557

25722558

pandas/core/indexes/period.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -692,19 +692,7 @@ def to_timestamp(self, freq=None, how='start'):
692692
base, mult = _gfc(freq)
693693
new_data = self.asfreq(freq, how)
694694

695-
end = how == 'E'
696-
if end:
697-
indexer = np.where(new_data.notnull())
698-
# move forward one period
699-
new_data._values[indexer] += 1
700-
ndarray_vals = new_data._ndarray_values
701-
new_data = period.periodarr_to_dt64arr(ndarray_vals, base)
702-
# subtract one nanosecond
703-
new_data[indexer] -= 1
704-
else:
705-
ndarray_vals = new_data._ndarray_values
706-
new_data = period.periodarr_to_dt64arr(ndarray_vals, base)
707-
695+
new_data = period.periodarr_to_dt64arr(new_data._ndarray_values, base)
708696
return DatetimeIndex(new_data, freq='infer', name=self.name)
709697

710698
def _maybe_convert_timedelta(self, other):

pandas/tests/indexes/period/test_period.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,13 @@ def test_periods_number_check(self):
367367
period_range('2011-1-1', '2012-1-1', 'B')
368368

369369
def test_start_time(self):
370+
# GH 17157
370371
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
371372
expected_index = date_range('2016-01-01', end='2016-05-31', freq='MS')
372373
tm.assert_index_equal(index.start_time, expected_index)
373374

374375
def test_end_time(self):
376+
# GH 17157
375377
index = PeriodIndex(freq='M', start='2016-01-01', end='2016-05-31')
376378
expected_index = date_range('2016-01-01', end='2016-05-31', freq='M')
377379
expected_index = expected_index.shift(1, freq='D').shift(-1, freq='ns')

0 commit comments

Comments
 (0)