Skip to content

No index labels on Dataframe plot with TimedeltaIndex #18910

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

Open
Sup3rGeo opened this issue Dec 22, 2017 · 6 comments
Open

No index labels on Dataframe plot with TimedeltaIndex #18910

Sup3rGeo opened this issue Dec 22, 2017 · 6 comments
Labels
Bug Regression Functionality that used to work in a prior pandas version Timedelta Timedelta data type Visualization plotting

Comments

@Sup3rGeo
Copy link
Contributor

Code Sample, a copy-pastable example if possible

Download the pickled Example Dataframe, which are basically two sine waves. Then just try to plot it:

import pandas
df = pandas.read_pickle("pd_example")
df.plot()

Problem description

There are no labels at all for the index (x-axis).

Expected Output

A while ago I had labels as "0 days 00:00:xxx.xxxxx", but it is not the case anymore.
Tested on a fresh installation with same results as I have now (no labels).

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.21.1
pytest: 3.3.2.dev44+gdb4df583
pip: 9.0.1
setuptools: 38.2.4
Cython: None
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.1
openpyxl: 1.7.0
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@sinhrks
Copy link
Member

sinhrks commented Dec 23, 2017

How is DataFrame created? Overwriting index looks work.

df.index = pd.to_timedelta(np.arange(len(df)), unit='us')
df.plot()
# OK

@sinhrks sinhrks added Timedelta Timedelta data type Visualization plotting labels Dec 23, 2017
@jreback jreback added the Needs Info Clarification about behavior needed to assess issue label Dec 23, 2017
@Sup3rGeo
Copy link
Contributor Author

Sup3rGeo commented Dec 29, 2017

Could you reproduce it using the dataframe as is? Does it mean that it is malformed somehow?

I am using this to generate the indexes:

>>>> time_line
array([  0.00000000e+00,   1.00000000e-06,   2.00000000e-06, ...,
     9.99997000e-01,   9.99998000e-01,   9.99999000e-01])

>>>> deltatime_line = pd.to_timedelta(time_line,unit="s")
>>>> df = pd.DataFrame(signal_matrix, columns=sig_names, index=deltatime_line)

@eldad-a
Copy link

eldad-a commented Feb 2, 2018

For the sake of those of needs a (quick and dirty) solution till this gets fixed:

import pandas
df = pandas.read_pickle("pd_example")
df.plot()

timeunit = 'us' # assuming it is given in micro-seconds

fig = plt.gcf()
ax_ = plt.gca()
labels = ax_.get_xticks().tolist()
new_labels = pd.to_timedelta(labels, unit=timeunit, box=True)
ax_.set_xticklabels(new_labels );
fig.autofmt_xdate()

image

@eldad-a
Copy link

eldad-a commented Feb 2, 2018

As a (non equivalent) alternative [based on this post]:

import matplotlib, datetime
from matplotlib import pyplot as plt

def timeTicks(x, pos):                                                                                                                                                                                                                                                         
    d = datetime.timedelta(microseconds=x)                                                                                                                                                                                                                         
    return str(d)                                                                                                                                                                                                                                                              

import pandas as pd
df = pd.read_pickle("pd_example")
ax = df.plot()

formatter = matplotlib.ticker.FuncFormatter(timeTicks)                                                                                                                                                                                                                         
ax.xaxis.set_major_formatter(formatter) 
plt.gcf().autofmt_xdate()

image

@jorisvandenbossche
Copy link
Member

Easier reproducible example:

seconds = np.arange(0, 1, 1e-4)
td_seconds = pd.to_timedelta(seconds, unit="s")
df = pd.DataFrame({'a': np.random.randn(len(td_seconds))}, index=td_seconds)
df.plot()

and with that I can confirm the regression.

If you increase the resolution from 1e-4 to 1e-2 (so > milliseconds), then it works correctly. I suppose this is related to the refactor of timedelta plot xlabel formatting that was done in 0.20 (#15067)

@jorisvandenbossche jorisvandenbossche added Regression Functionality that used to work in a prior pandas version and removed Needs Info Clarification about behavior needed to assess issue labels Feb 2, 2018
@jorisvandenbossche jorisvandenbossche added this to the 0.23.0 milestone Feb 2, 2018
@jreback jreback modified the milestones: 0.23.0, Next Major Release Apr 14, 2018
@KKostya
Copy link

KKostya commented Oct 28, 2019

I must add that the whole timdelta index plotting is broken

td_seconds = pd.to_timedelta([0,1,2,10,11,12], unit="s")
df = pd.DataFrame({'a': np.random.randn(len(td_seconds))}, index=td_seconds)
df.plot(marker='o')

@mroeschke mroeschke added the Bug label May 3, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Regression Functionality that used to work in a prior pandas version Timedelta Timedelta data type Visualization plotting
Projects
None yet
Development

No branches or pull requests

7 participants