Skip to content

Commit f9e9fbd

Browse files
committed
simplify timestamp precision issue
1 parent c4db736 commit f9e9fbd

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
@@ -538,12 +538,12 @@ Returning a ``Series`` allows one to control the exact return structure and colu
538538
.. _whatsnew_0230.api_breaking.end_time:
539539

540540
Time values in ``dt.end_time`` and ``to_timestamp(how='end')``
541-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
541+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
542542

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

548548
Previous Behavior:
549549

@@ -555,22 +555,11 @@ Previous Behavior:
555555
In [4]: pd.Series(p).dt.end_time[0]
556556
Out[4]: Timestamp(2017-01-01 00:00:00)
557557

558-
In [5]: pd.Series(pi).dt.end_time[0]
558+
In [5]: pi.end_time[0]
559559
Out[5]: Timestamp(2017-01-01 00:00:00)
560560

561-
In [6]: p.end_time
562-
Out[6]: Timestamp(2017-01-01 23:59:59.999999999)
563-
564-
In [7]: pi.end_time[0]
565-
Out[7]: Timestamp(2017-01-01 00:00:00)
566-
567-
In [8]: p.to_timestamp(how='end')
568-
Out[8]: Timestamp(2017-01-01 00:00:00)
569-
570-
In [9]: pi.to_timestamp(how='end')[0]
571-
Out[9]: Timestamp(2017-01-01 00:00:00)
572-
573-
Current Behavior
561+
In [6]: pi.to_timestamp(how='end')
562+
Out[6]: Timestamp(2017-01-01 00:00:00)
574563

575564
.. ipython:: python
576565

@@ -579,15 +568,9 @@ Current Behavior
579568

580569
pd.Series(p).dt.end_time[0]
581570

582-
pd.Series(pi).dt.end_time[0]
583-
584-
p.end_time
585-
586571
pi.end_time[0]
587572

588-
p.to_timestamp(how='end')
589-
590-
pi.to_timestamp(how='end')[0]
573+
pi.to_timestamp(how='end')
591574

592575

593576
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

@@ -2556,28 +2556,14 @@ def _generate_regular_range(start, end, periods, offset):
25562556
data = np.arange(b, e, stride, dtype=np.int64)
25572557
data = DatetimeIndex._simple_new(data, None, tz=tz)
25582558
else:
2559-
if isinstance(start, Timestamp):
2560-
start_date = start.to_pydatetime()
2561-
else:
2562-
start_date = start
2563-
2564-
if isinstance(end, Timestamp):
2565-
end_date = end.to_pydatetime()
2566-
else:
2567-
end_date = end
25682559

2569-
xdr = generate_range(start=start_date, end=end_date,
2560+
xdr = generate_range(start=start, end=end,
25702561
periods=periods, offset=offset)
25712562

25722563
dates = list(xdr)
25732564
# utc = len(dates) > 0 and dates[0].tzinfo is not None
25742565
data = tools.to_datetime(dates)
25752566

2576-
# Add back in the lost nanoseconds
2577-
if isinstance(start, Timestamp) and isinstance(end, Timestamp):
2578-
if start.nanosecond == 999 and end.nanosecond == 999:
2579-
data = data + Timedelta(999, 'ns')
2580-
25812567
return data
25822568

25832569

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)