pandas.DataFrame.plot error for time series data · Issue #26921 · pandas-dev/pandas (original) (raw)

Code Sample

import pandas as pd

idx = pd.date_range(start="2018", freq=pd.DateOffset(days=1), periods=2) ts = pd.DataFrame({"A": [1, 2]}, idx) ts.plot()

Problem description

I created the index of a time series using pd.date_range and provided the frequency as a DateOffset object. This index was passed to the DataFrame constructor to create a time series. When calling the plot method on this time series, pandas is not able to parse the frequency correctly:

233     if isinstance(freq, DateOffset):
--> 234         freq = freq.rule_code
    235     else:
    236         freq = get_base_alias(freq)

~/venv/lib/python3.6/site-packages/pandas/tseries/offsets.py in rule_code(self)
    372     @property
    373     def rule_code(self):
--> 374         return self._prefix
    375 
    376     @cache_readonly

~venv/lib/python3.6/site-packages/pandas/tseries/offsets.py in _prefix(self)
    368     @property
    369     def _prefix(self):
--> 370         raise NotImplementedError('Prefix not defined')
    371 
    372     @property

Expected Output

When passing the frequency to pd.date_range as a string, everything works fine, however both indices are equal.

idx2 = pd.date_range(start="2018", freq="D", periods=2) ts = pd.DataFrame({"A": [1, 2]}, idx2) ts.plot() idx.equals(idx2)

Output: True

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.7.final.0
python-bits: 64
OS: Linux
OS-release: 4.18.0-10-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 4.3.1
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.29.6
numpy: 1.16.2
scipy: None
pyarrow: None
xarray: None
IPython: 7.3.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: 4.7.1
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None