Skip to content

Commit 8dc4e7f

Browse files
committed
simplify timestamp precision issue
1 parent ed96908 commit 8dc4e7f

File tree

4 files changed

+12
-58
lines changed

4 files changed

+12
-58
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -780,12 +780,12 @@ Note that this change also applies to :meth:`DataFrame.append`, which has also r
780780
.. _whatsnew_0230.api_breaking.end_time:
781781

782782
Time values in ``dt.end_time`` and ``to_timestamp(how='end')``
783-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
783+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
784784

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

790790
Previous Behavior:
791791

@@ -797,22 +797,11 @@ Previous Behavior:
797797
In [4]: pd.Series(p).dt.end_time[0]
798798
Out[4]: Timestamp(2017-01-01 00:00:00)
799799

800-
In [5]: pd.Series(pi).dt.end_time[0]
800+
In [5]: pi.end_time[0]
801801
Out[5]: Timestamp(2017-01-01 00:00:00)
802802

803-
In [6]: p.end_time
804-
Out[6]: Timestamp(2017-01-01 23:59:59.999999999)
805-
806-
In [7]: pi.end_time[0]
807-
Out[7]: Timestamp(2017-01-01 00:00:00)
808-
809-
In [8]: p.to_timestamp(how='end')
810-
Out[8]: Timestamp(2017-01-01 00:00:00)
811-
812-
In [9]: pi.to_timestamp(how='end')[0]
813-
Out[9]: Timestamp(2017-01-01 00:00:00)
814-
815-
Current Behavior
803+
In [6]: pi.to_timestamp(how='end')
804+
Out[6]: Timestamp(2017-01-01 00:00:00)
816805

817806
.. ipython:: python
818807

@@ -821,15 +810,9 @@ Current Behavior
821810

822811
pd.Series(p).dt.end_time[0]
823812

824-
pd.Series(pi).dt.end_time[0]
825-
826-
p.end_time
827-
828813
pi.end_time[0]
829814

830-
p.to_timestamp(how='end')
831-
832-
pi.to_timestamp(how='end')[0]
815+
pi.to_timestamp(how='end')
833816

834817

835818
Build Changes

pandas/core/indexes/datetimes.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@
4949
import pandas.tseries.offsets as offsets
5050
import pandas.core.tools.datetimes as tools
5151

52-
<<<<<<< 85e80d8f8813917c299c23d15deeb00898f6c1f0
5352
from pandas._libs import (lib, index as libindex, tslibs, tslib as libts,
5453
join as libjoin, Timestamp)
55-
=======
56-
from pandas._libs import (lib, index as libindex, tslib as libts,
57-
join as libjoin, Timestamp, Timedelta)
58-
>>>>>>> try to fix failing tests
5954
from pandas._libs.tslibs import (timezones, conversion, fields, parsing,
6055
ccalendar)
6156

@@ -1795,28 +1790,14 @@ def _generate_regular_range(start, end, periods, freq):
17951790
data = np.arange(b, e, stride, dtype=np.int64)
17961791
data = DatetimeIndex._simple_new(data.view(_NS_DTYPE), None, tz=tz)
17971792
else:
1798-
if isinstance(start, Timestamp):
1799-
start_date = start.to_pydatetime()
1800-
else:
1801-
start_date = start
1802-
1803-
if isinstance(end, Timestamp):
1804-
end_date = end.to_pydatetime()
1805-
else:
1806-
end_date = end
18071793

1808-
xdr = generate_range(start=start_date, end=end_date,
1794+
xdr = generate_range(start=start, end=end,
18091795
periods=periods, offset=offset)
18101796

18111797
dates = list(xdr)
18121798
# utc = len(dates) > 0 and dates[0].tzinfo is not None
18131799
data = tools.to_datetime(dates)
18141800

1815-
# Add back in the lost nanoseconds
1816-
if isinstance(start, Timestamp) and isinstance(end, Timestamp):
1817-
if start.nanosecond == 999 and end.nanosecond == 999:
1818-
data = data + Timedelta(999, 'ns')
1819-
18201801
return data
18211802

18221803

pandas/core/indexes/period.py

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

525-
end = how == 'E'
526-
if end:
527-
indexer = np.where(new_data.notnull())
528-
# move forward one period
529-
new_data._values[indexer] += 1
530-
ndarray_vals = new_data._ndarray_values
531-
new_data = period.periodarr_to_dt64arr(ndarray_vals, base)
532-
# subtract one nanosecond
533-
new_data[indexer] -= 1
534-
else:
535-
ndarray_vals = new_data._ndarray_values
536-
new_data = period.periodarr_to_dt64arr(ndarray_vals, base)
537-
525+
new_data = period.periodarr_to_dt64arr(new_data._ndarray_values, base)
538526
return DatetimeIndex(new_data, freq='infer', name=self.name)
539527

540528
@property

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)