Skip to content

Commit d98dfe8

Browse files
author
Tom Farnbauer
committed
ENH: (GH3863) Timestamp.min and Timestamp.max return a valid Timestamp
1 parent 3349ea7 commit d98dfe8

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ pandas 0.11.1
6969
- support python3 (via ``PyTables 3.0.0``) (GH3750_)
7070
- Add modulo operator to Series, DataFrame
7171
- Add ``date`` method to DatetimeIndex
72+
- Timestamp.min and Timestamp.max now represent valid Timestamp instances instead
73+
of the default datetime.min and datetime.max (respectively).
7274
- Simplified the API and added a describe method to Categorical
7375
- ``melt`` now accepts the optional parameters ``var_name`` and ``value_name``
7476
to specify custom column names of the returned DataFrame (GH3649_),

doc/source/gotchas.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,10 @@ can be represented using a 64-bit integer is limited to approximately 584 years:
271271

272272
.. ipython:: python
273273
274-
begin = Timestamp(-9223285636854775809L)
274+
begin = Timestamp.min
275275
begin
276-
end = Timestamp(np.iinfo(np.int64).max)
276+
277+
end = Timestamp.max
277278
end
278279
279280
If you need to represent time series data outside the nanosecond timespan, use

doc/source/v0.11.1.txt

+4
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,12 @@ Enhancements
289289
dff.groupby('B').filter(lambda x: len(x) > 2, dropna=False)
290290

291291
- Series and DataFrame hist methods now take a ``figsize`` argument (GH3834_)
292+
292293
- DatetimeIndexes no longer try to convert mixed-integer indexes during join
293294
operations (GH3877_)
295+
296+
- Timestamp.min and Timestamp.max now represent valid Timestamp instances instead
297+
of the default datetime.min and datetime.max (respectively).
294298

295299

296300
Bug Fixes

pandas/tests/test_tseries.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from numpy import nan
44
import numpy as np
5-
from pandas import Index, isnull
5+
from pandas import Index, isnull, Timestamp
66
from pandas.util.testing import assert_almost_equal
77
import pandas.util.testing as common
88
import pandas.lib as lib
@@ -683,6 +683,22 @@ def test_int_index(self):
683683
expected = arr.sum(1)
684684
assert_almost_equal(result, expected)
685685

686+
687+
class TestTsUtil(unittest.TestCase):
688+
def test_min_valid(self):
689+
# Ensure that Timestamp.min is a valid Timestamp
690+
Timestamp(Timestamp.min)
691+
692+
def test_max_valid(self):
693+
# Ensure that Timestamp.max is a valid Timestamp
694+
Timestamp(Timestamp.max)
695+
696+
def test_to_datetime_bijective(self):
697+
# Ensure that converting to datetime and back only loses precision
698+
# by going from nanoseconds to microseconds.
699+
self.assertEqual(Timestamp(Timestamp.max.to_pydatetime()).value/1000, Timestamp.max.value/1000)
700+
self.assertEqual(Timestamp(Timestamp.min.to_pydatetime()).value/1000, Timestamp.min.value/1000)
701+
686702
if __name__ == '__main__':
687703
import nose
688704
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/tslib.pyx

+10-3
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,15 @@ cpdef object get_value_box(ndarray arr, object loc):
388388
return util.get_value_1d(arr, i)
389389

390390

391+
# Add the min and max fields at the class level
392+
# These are defined as magic numbers due to strange
393+
# wraparound behavior when using the true int64 lower boundary
394+
cdef int64_t _NS_LOWER_BOUND = -9223285636854775000LL
395+
cdef int64_t _NS_UPPER_BOUND = 9223372036854775807LL
396+
Timestamp.min = Timestamp(_NS_LOWER_BOUND)
397+
Timestamp.max = Timestamp(_NS_UPPER_BOUND)
398+
399+
391400
#----------------------------------------------------------------------
392401
# Frequency inference
393402

@@ -769,8 +778,6 @@ cdef inline object _get_zone(object tz):
769778
except AttributeError:
770779
return tz
771780

772-
# cdef int64_t _NS_LOWER_BOUND = -9223285636854775809LL
773-
# cdef int64_t _NS_UPPER_BOUND = -9223372036854775807LL
774781

775782
cdef inline _check_dts_bounds(int64_t value, pandas_datetimestruct *dts):
776783
cdef pandas_datetimestruct dts2
@@ -2868,4 +2875,4 @@ def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
28682875
return 1 + days_to_week + day_of_week
28692876

28702877
# def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
2871-
# return _strptime(data_string, format)[0]
2878+
# return _strptime(data_string, format)[0]

0 commit comments

Comments
 (0)