Skip to content

ENH: (GH3863) Timestamp.min and Timestamp.max return a valid Timestamp #3902

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 1 commit into from
Jul 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pandas 0.11.1
- support python3 (via ``PyTables 3.0.0``) (GH3750_)
- Add modulo operator to Series, DataFrame
- Add ``date`` method to DatetimeIndex
- Timestamp.min and Timestamp.max now represent valid Timestamp instances instead
of the default datetime.min and datetime.max (respectively).
- Simplified the API and added a describe method to Categorical
- ``melt`` now accepts the optional parameters ``var_name`` and ``value_name``
to specify custom column names of the returned DataFrame (GH3649_),
Expand Down
5 changes: 3 additions & 2 deletions doc/source/gotchas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ can be represented using a 64-bit integer is limited to approximately 584 years:

.. ipython:: python

begin = Timestamp(-9223285636854775809L)
begin = Timestamp.min
begin
end = Timestamp(np.iinfo(np.int64).max)

end = Timestamp.max
end

If you need to represent time series data outside the nanosecond timespan, use
Expand Down
4 changes: 4 additions & 0 deletions doc/source/v0.11.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,12 @@ Enhancements
dff.groupby('B').filter(lambda x: len(x) > 2, dropna=False)

- Series and DataFrame hist methods now take a ``figsize`` argument (GH3834_)

- DatetimeIndexes no longer try to convert mixed-integer indexes during join
operations (GH3877_)

- Timestamp.min and Timestamp.max now represent valid Timestamp instances instead
of the default datetime.min and datetime.max (respectively).


Bug Fixes
Expand Down
18 changes: 17 additions & 1 deletion pandas/tests/test_tseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from numpy import nan
import numpy as np
from pandas import Index, isnull
from pandas import Index, isnull, Timestamp
from pandas.util.testing import assert_almost_equal
import pandas.util.testing as common
import pandas.lib as lib
Expand Down Expand Up @@ -683,6 +683,22 @@ def test_int_index(self):
expected = arr.sum(1)
assert_almost_equal(result, expected)


class TestTsUtil(unittest.TestCase):
def test_min_valid(self):
# Ensure that Timestamp.min is a valid Timestamp
Timestamp(Timestamp.min)

def test_max_valid(self):
# Ensure that Timestamp.max is a valid Timestamp
Timestamp(Timestamp.max)

def test_to_datetime_bijective(self):
# Ensure that converting to datetime and back only loses precision
# by going from nanoseconds to microseconds.
self.assertEqual(Timestamp(Timestamp.max.to_pydatetime()).value/1000, Timestamp.max.value/1000)
self.assertEqual(Timestamp(Timestamp.min.to_pydatetime()).value/1000, Timestamp.min.value/1000)

if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down
13 changes: 10 additions & 3 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,15 @@ cpdef object get_value_box(ndarray arr, object loc):
return util.get_value_1d(arr, i)


# Add the min and max fields at the class level
# These are defined as magic numbers due to strange
# wraparound behavior when using the true int64 lower boundary
cdef int64_t _NS_LOWER_BOUND = -9223285636854775000LL
cdef int64_t _NS_UPPER_BOUND = 9223372036854775807LL
Timestamp.min = Timestamp(_NS_LOWER_BOUND)
Timestamp.max = Timestamp(_NS_UPPER_BOUND)


#----------------------------------------------------------------------
# Frequency inference

Expand Down Expand Up @@ -769,8 +778,6 @@ cdef inline object _get_zone(object tz):
except AttributeError:
return tz

# cdef int64_t _NS_LOWER_BOUND = -9223285636854775809LL
# cdef int64_t _NS_UPPER_BOUND = -9223372036854775807LL

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

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