Skip to content

BUG: Fix the un-pickleable plot with DatetimeIndex #18486

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
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: 1 addition & 1 deletion doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ I/O
Plotting
^^^^^^^^

-
- Bug in ``DataFrame.plot()`` and ``Series.plot()`` with :class:`DatetimeIndex` where a figure generated by them is not pickleable in Python 3 (:issue:`18439`)
-
-

Expand Down
9 changes: 7 additions & 2 deletions pandas/plotting/_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# TODO: Use the fact that axis can have units to simplify the process

import functools

import numpy as np

from matplotlib import pylab
Expand Down Expand Up @@ -293,6 +295,10 @@ def format_timedelta_ticks(x, pos, n_decimals):
return s


def _format_coord(freq, t, y):
return "t = {0} y = {1:8f}".format(Period(ordinal=int(t), freq=freq), y)


def format_dateaxis(subplot, freq, index):
"""
Pretty-formats the date axis (x-axis).
Expand Down Expand Up @@ -327,8 +333,7 @@ def format_dateaxis(subplot, freq, index):
subplot.xaxis.set_minor_formatter(minformatter)

# x and y coord info
subplot.format_coord = lambda t, y: (
"t = {0} y = {1:8f}".format(Period(ordinal=int(t), freq=freq), y))
subplot.format_coord = functools.partial(_format_coord, freq)

elif isinstance(index, TimedeltaIndex):
subplot.xaxis.set_major_formatter(
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
""" Test cases for time series specific (freq conversion, etc) """

from datetime import datetime, timedelta, date, time
import pickle

import pytest
from pandas.compat import lrange, zip

import numpy as np
from pandas import Index, Series, DataFrame, NaT
from pandas.compat import is_platform_mac
from pandas.compat import is_platform_mac, PY3
from pandas.core.indexes.datetimes import date_range, bdate_range
from pandas.core.indexes.timedeltas import timedelta_range
from pandas.tseries.offsets import DateOffset
Expand Down Expand Up @@ -1470,5 +1471,12 @@ def _check_plot_works(f, freq=None, series=None, *args, **kwargs):

with ensure_clean(return_filelike=True) as path:
plt.savefig(path)

# GH18439
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this only PY3?

add a more informative comment, not just the issue, e.g. ensure round-trip pickle compat

Copy link
Contributor

Choose a reason for hiding this comment

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

@Licht-T saw your comment, ok then, just make a comment that we are pickling instancemethods which is supported on PY3 only (hmm thought this did work on PY2)

# this is supported only in Python 3 pickle since
# pickle in Python2 doesn't support instancemethod pickling
if PY3:
with ensure_clean(return_filelike=True) as path:
pickle.dump(fig, path)
finally:
plt.close(fig)