Skip to content

Commit c763ecf

Browse files
Licht-TTomAugspurger
authored andcommitted
BUG: Fix the un-pickleable plot with DatetimeIndex (#18486)
1 parent e823f77 commit c763ecf

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ I/O
9393
Plotting
9494
^^^^^^^^
9595

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

pandas/plotting/_timeseries.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# TODO: Use the fact that axis can have units to simplify the process
22

3+
import functools
4+
35
import numpy as np
46

57
from matplotlib import pylab
@@ -293,6 +295,10 @@ def format_timedelta_ticks(x, pos, n_decimals):
293295
return s
294296

295297

298+
def _format_coord(freq, t, y):
299+
return "t = {0} y = {1:8f}".format(Period(ordinal=int(t), freq=freq), y)
300+
301+
296302
def format_dateaxis(subplot, freq, index):
297303
"""
298304
Pretty-formats the date axis (x-axis).
@@ -327,8 +333,7 @@ def format_dateaxis(subplot, freq, index):
327333
subplot.xaxis.set_minor_formatter(minformatter)
328334

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

333338
elif isinstance(index, TimedeltaIndex):
334339
subplot.xaxis.set_major_formatter(

pandas/tests/plotting/test_datetimelike.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
""" Test cases for time series specific (freq conversion, etc) """
22

33
from datetime import datetime, timedelta, date, time
4+
import pickle
45

56
import pytest
67
from pandas.compat import lrange, zip
78

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

14711472
with ensure_clean(return_filelike=True) as path:
14721473
plt.savefig(path)
1474+
1475+
# GH18439
1476+
# this is supported only in Python 3 pickle since
1477+
# pickle in Python2 doesn't support instancemethod pickling
1478+
if PY3:
1479+
with ensure_clean(return_filelike=True) as path:
1480+
pickle.dump(fig, path)
14731481
finally:
14741482
plt.close(fig)

0 commit comments

Comments
 (0)