Repository: pandas-dev/pandas
Pull request: TST: Period with Timestamp overflow
- [x] closes #13346
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`

The first part of the test checks that the period inside boundaries does not raise exception when calling `start_time` and `end_time` methods. The second part checks that methods raise exceptions when outside of boundaries.
The parameters are `Timestamp.min` and `Timestamp.max`, so the tests will follow any change of the boundaries.


Problem statement:
pd.Period off by hundred of years for dates past ~2263
```
In [1]: import pandas as pd

In [2]: pd.Period(year=2300, month=1, day=1, freq='M').end_time
Out[2]: Timestamp('1715-07-14 00:25:26.290448383')
```

Input year is '2300', output shows '1715'
#### output of `pd.show_versions()`

In [3]: pd.show_versions()
## INSTALLED VERSIONS

commit: None
python: 2.7.11.final.0
python-bits: 64
OS: Linux
OS-release: 3.19.0-25-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: None
pip: 7.1.2
setuptools: 21.0.0
Cython: None
numpy: 1.11.0
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: None
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: 0.9999999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.39.0
pandas_datareader: 0.2.1

Hints from issue discussion:
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#timestamp-limitations

there are limitations on the repr of Timestamps.

you can represent as `Periods` that is in fact the point. But conversions are out-of-bounds.

I suppose this should actually raise.

cc @MaximilianR 
@sinhrks 

+1 for raising an Exception

Is another way to get the year-month-day corresponding to end_time/start_time of the given period?

actually this should catch the oob error and just return a datetime

Don't have enough context about how pd.Period is used throughout pandas... 

But having the return type change based on a non-obvious criteria could lead to problems as well. If possible would be nice to get a consistent type out (datetime).

Timestamp is a subclass of datetime
you need to be cognizant of the limits 

Currently offsets coerces to normal `datetime`. As `Period` can be used as a workaround for `Timestamp` limitation, I prefer returning normal datetime when overflow.

```
pd.Timestamp('2011-01-01') + pd.offsets.YearEnd(1000) 
# datetime.datetime(3010, 12, 31, 0, 0)
```

Could `Period` be switched to `datetime64`? https://github.com/pydata/pandas/issues/179

Commit message: #TST #13346 added tests

Patch to review:
diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py
index 702899f163e06..34f8d05419bf5 100644
--- a/pandas/tests/scalar/period/test_period.py
+++ b/pandas/tests/scalar/period/test_period.py
@@ -6,6 +6,7 @@
 
 from pandas._libs.tslibs import iNaT, period as libperiod
 from pandas._libs.tslibs.ccalendar import DAYS, MONTHS
+from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
 from pandas._libs.tslibs.parsing import DateParseError
 from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG, IncompatibleFrequency
 from pandas._libs.tslibs.timezones import dateutil_gettz, maybe_get_tz
@@ -767,6 +768,34 @@ def test_period_deprecated_freq(self):
             assert isinstance(p1, Period)
             assert isinstance(p2, Period)
 
+    @pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)])
+    def test_start_time_and_end_time_bounds(self, bound, offset):
+        # GH #13346
+        period = Period(
+            year=bound.year,
+            month=bound.month,
+            day=bound.day,
+            hour=bound.hour,
+            minute=bound.minute,
+            second=bound.second - offset,
+            freq="us",
+        )
+        period.start_time
+        period.end_time
+        period = Period(
+            year=bound.year,
+            month=bound.month,
+            day=bound.day,
+            hour=bound.hour,
+            minute=bound.minute,
+            second=bound.second + offset,
+            freq="us",
+        )
+        with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"):
+            period.start_time
+        with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"):
+            period.end_time
+
     def test_start_time(self):
         freq_lst = ["A", "Q", "M", "D", "H", "T", "S"]
         xp = datetime(2012, 1, 1)


Code review comments:
diff_hunk: @@ -767,6 +768,34 @@ def test_period_deprecated_freq(self):
             assert isinstance(p1, Period)
             assert isinstance(p2, Period)
 
+    @pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)])
+    def test_start_time_and_end_time_bounds(self, bound, offset):
+        # GH #13346
+        period = Period(
+            year=bound.year,
+            month=bound.month,
+            day=bound.day,
+            hour=bound.hour,
+            minute=bound.minute,
+            second=bound.second - offset,
+            freq="us",
+        )
+        period.start_time
path: pandas/tests/scalar/period/test_period.py
text: @user1:
Would be great to assert what this operation returns. `assert period.start_time == ...`

@author:
I've tried write a better line for this one, but didn't managed.