Skip to content

BUG: handle outofbounds datetimes in DatetimeConverter #13801

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

Closed
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
8 changes: 8 additions & 0 deletions pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,14 @@ def test_secondary_y_irregular_ts_xlim(self):
self.assertEqual(left, ts_irregular.index.min().toordinal())
self.assertEqual(right, ts_irregular.index.max().toordinal())

def test_plot_outofbounds_datetime(self):
Copy link
Contributor

@tacaswell tacaswell Jul 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does pandas have the equivalent of @cleanup from the mpl tests? This is touching global state and can leak to other tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In TestPlotBase (from which this testcase inherits), all settings are set to the defaul in the setup, and the figures closed in the teardown.
Or are there still other things that may leak?

# 2579 - checking this does not raise
values = [date(1677, 1, 1), date(1677, 1, 2)]
self.plt.plot(values)

values = [datetime(1677, 1, 1, 12), datetime(1677, 1, 2, 12)]
self.plt.plot(values)


def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
import matplotlib.pyplot as plt
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def try_parse(values):
else:
values = [_dt_to_float_ordinal(x) for x in values]
except Exception:
pass
values = _dt_to_float_ordinal(values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow me I do not understand converter dispatch logic.

Might be unrelated to the PR, but is it possible that input can have a type not registered for DatetimeConverter like below condition? If so, it may better to box the above with try-except to return input as it is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not completely sure, but by letting the DatetimeConverter accept for example strings (although string type is not registered), this makes it possible to do ax.set_xlim('2012-01-01', '2012-01-10')


return values

Expand Down
18 changes: 18 additions & 0 deletions pandas/tseries/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ def test_conversion_float(self):
rs = self.dtc.convert(datetime(2012, 1, 1, 1, 2, 3), None, None)
tm.assert_almost_equal(rs, xp, decimals)

def test_conversion_outofbounds_datetime(self):
# 2579
values = [date(1677, 1, 1), date(1677, 1, 2)]
rs = self.dtc.convert(values, None, None)
xp = converter.dates.date2num(values)
tm.assert_numpy_array_equal(rs, xp)
rs = self.dtc.convert(values[0], None, None)
xp = converter.dates.date2num(values[0])
self.assertEqual(rs, xp)

values = [datetime(1677, 1, 1, 12), datetime(1677, 1, 2, 12)]
rs = self.dtc.convert(values, None, None)
xp = converter.dates.date2num(values)
tm.assert_numpy_array_equal(rs, xp)
rs = self.dtc.convert(values[0], None, None)
xp = converter.dates.date2num(values[0])
self.assertEqual(rs, xp)

def test_time_formatter(self):
self.tc(90000)

Expand Down