Skip to content
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

Plotting Timedelta on y-axis #16953

Closed
scls19fr opened this issue Jul 15, 2017 · 6 comments · Fixed by #17430
Closed

Plotting Timedelta on y-axis #16953

scls19fr opened this issue Jul 15, 2017 · 6 comments · Fixed by #17430
Labels
Milestone

Comments

@scls19fr
Copy link
Contributor

scls19fr commented Jul 15, 2017

Code Sample

import pandas as pd
from pandas.compat import StringIO
import matplotlib.pyplot as plt


dat = """c1,c2,c3
1000,2000,1500
9000,8000,1600"""

df = pd.read_csv(StringIO(dat))

print(df)

df.plot.bar()
plt.show()

df = df.apply(lambda x: pd.to_timedelta(x, unit='ms'))

print(df)

df.plot.bar()
plt.show()

Problem description

plot.bar() works with numerical values (int, float).

     c1    c2    c3
0  1000  2000  1500
1  9000  8000  1600

capture d ecran 2017-07-15 a 20 20 30

not with Timedelta

        c1       c2              c3
0 00:00:01 00:00:02 00:00:01.500000
1 00:00:09 00:00:08 00:00:01.600000
    df.plot.bar()
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 2659, in bar
    return self(kind='bar', x=x, y=y, **kwds)
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 2620, in __call__
    sort_columns=sort_columns, **kwds)
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 1857, in plot_frame
    **kwds)
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 1682, in _plot
    plot_obj.generate()
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 236, in generate
    self._compute_plot_data()
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 345, in _compute_plot_data
    'plot'.format(numeric_data.__class__.__name__))
TypeError: Empty 'DataFrame': no numeric data to plot

Expected Output

Timedelta on y axis should be displayed

Output of pd.show_versions()

/Users/scls/anaconda/lib/python3.6/site-packages/xarray/core/formatting.py:16: FutureWarning: The pandas.tslib module is deprecated and will be removed in a future version. from pandas.tslib import OutOfBoundsDatetime

INSTALLED VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Darwin
OS-release: 16.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: fr_FR.UTF-8
LOCALE: fr_FR.UTF-8

pandas: 0.20.1
pytest: 3.1.2
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: 0.9.5
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.3.0
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: 0.4.0

@TomAugspurger
Copy link
Contributor

I wonder if we could change

numeric_data = data._convert(datetime=True)._get_numeric_data()
to include timedelta=True? Maybe try changing that and seeing what breaks.

@scls19fr
Copy link
Contributor Author

After changing

    numeric_data = data._convert(datetime=True)._get_numeric_data()

to

    numeric_data = data._convert(datetime=True, timedelta=True)._get_numeric_data()

it's still raising same exception

@imih
Copy link
Contributor

imih commented Jul 16, 2017

The dtype of data._convert(datetime=True, timedelta=True).values is 'timedelta64[ns]' and they get filtered out by _get_numeric_data() because TimeDeltaBlock.is_numeric = False.
https://github.com/pandas-dev/pandas/blob/master/pandas/core/internals.py#L1705

numeric_data = data._convert(datetime=True, timedelta=True)
sort of works if you're fine with losing the timedelta format representation.

Got:
figure_2

@scls19fr
Copy link
Contributor Author

Plotting datetime64 on y-axis is also an issue.

import pandas as pd
from pandas.compat import StringIO
import matplotlib.pyplot as plt


dat = """c1,c2,c3
1000,2000,1500
9000,8000,1600"""

df = pd.read_csv(StringIO(dat))

print(df)
print(df.dtypes)

# df.plot.bar()
# plt.show()

df = df.apply(lambda x: pd.to_timedelta(x, unit='ms'))
for col in df.columns:
    df[col] = df[col] + pd.to_datetime('1970/01/01')

print(df)
print(df.dtypes)

df.plot.bar()
plt.show()

see

$ python mwe_pandas.py
     c1    c2    c3
0  1000  2000  1500
1  9000  8000  1600
c1    int64
c2    int64
c3    int64
dtype: object
                   c1                  c2                      c3
0 1970-01-01 00:00:01 1970-01-01 00:00:02 1970-01-01 00:00:01.500
1 1970-01-01 00:00:09 1970-01-01 00:00:08 1970-01-01 00:00:01.600
c1    datetime64[ns]
c2    datetime64[ns]
c3    datetime64[ns]
dtype: object
Traceback (most recent call last):
  File "mwe_pandas.py", line 27, in <module>
    df.plot.bar()
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 2659, in bar
    return self(kind='bar', x=x, y=y, **kwds)
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 2620, in __call__
    sort_columns=sort_columns, **kwds)
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 1857, in plot_frame
    **kwds)
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 1682, in _plot
    plot_obj.generate()
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 236, in generate
    self._compute_plot_data()
  File "/Users/scls/anaconda/lib/python3.6/site-packages/pandas/plotting/_core.py", line 345, in _compute_plot_data
    'plot'.format(numeric_data.__class__.__name__))
TypeError: Empty 'DataFrame': no numeric data to plot

s-weigand added a commit to s-weigand/pandas that referenced this issue Sep 4, 2017
s-weigand added a commit to s-weigand/pandas that referenced this issue Sep 4, 2017
TomAugspurger pushed a commit that referenced this issue Sep 6, 2017
* implemented fix for GH issue #16953

* added tests for fix of issue #16953

* changed comments for git issue to pandas style GH#

* changed linelength in tests, so all lines are less than 80 characters

* added whatsnew entry

* swaped conversion and filtering of values, for plot to also work with object dtypes

* refomated code, so len(line) < 80

* changed whatsnew with timedelta and datetime dtypes

* added support for datetimetz and extended tests

* added reason to pytest.mark.xfail
@scls19fr
Copy link
Contributor Author

scls19fr commented Sep 6, 2017

Thanks @s-weigand

jbrockmendel pushed a commit to jbrockmendel/pandas that referenced this issue Sep 10, 2017
* implemented fix for GH issue pandas-dev#16953

* added tests for fix of issue pandas-dev#16953

* changed comments for git issue to pandas style GH#

* changed linelength in tests, so all lines are less than 80 characters

* added whatsnew entry

* swaped conversion and filtering of values, for plot to also work with object dtypes

* refomated code, so len(line) < 80

* changed whatsnew with timedelta and datetime dtypes

* added support for datetimetz and extended tests

* added reason to pytest.mark.xfail
@scls19fr
Copy link
Contributor Author

scls19fr commented Sep 15, 2017

I tested today latest Pandas version (master)
I noticed that
#17430 fixed TypeError and now this script doesn't break but Timedelta representation on y-axis should be improved.
See #17553

jowens pushed a commit to jowens/pandas that referenced this issue Sep 20, 2017
* implemented fix for GH issue pandas-dev#16953

* added tests for fix of issue pandas-dev#16953

* changed comments for git issue to pandas style GH#

* changed linelength in tests, so all lines are less than 80 characters

* added whatsnew entry

* swaped conversion and filtering of values, for plot to also work with object dtypes

* refomated code, so len(line) < 80

* changed whatsnew with timedelta and datetime dtypes

* added support for datetimetz and extended tests

* added reason to pytest.mark.xfail
alanbato pushed a commit to alanbato/pandas that referenced this issue Nov 10, 2017
* implemented fix for GH issue pandas-dev#16953

* added tests for fix of issue pandas-dev#16953

* changed comments for git issue to pandas style GH#

* changed linelength in tests, so all lines are less than 80 characters

* added whatsnew entry

* swaped conversion and filtering of values, for plot to also work with object dtypes

* refomated code, so len(line) < 80

* changed whatsnew with timedelta and datetime dtypes

* added support for datetimetz and extended tests

* added reason to pytest.mark.xfail
No-Stream pushed a commit to No-Stream/pandas that referenced this issue Nov 28, 2017
* implemented fix for GH issue pandas-dev#16953

* added tests for fix of issue pandas-dev#16953

* changed comments for git issue to pandas style GH#

* changed linelength in tests, so all lines are less than 80 characters

* added whatsnew entry

* swaped conversion and filtering of values, for plot to also work with object dtypes

* refomated code, so len(line) < 80

* changed whatsnew with timedelta and datetime dtypes

* added support for datetimetz and extended tests

* added reason to pytest.mark.xfail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants