Skip to content

BUG: Plotting Timedelta on y-axis #16953 #17430

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 10 commits into from
Sep 6, 2017

Conversation

s-weigand
Copy link
Contributor

This fixes the issue with a TypeError being raised if the y-data were of type Timedelta or Datetime.
The plot of Timedelta doesn't look pretty since it is converted to ns representation, but matplotlib has already an open issue for that.

PS: Greetings to the people from the EuroSciPy 2017 sprints ^^

@gfyoung gfyoung added the Visualization plotting label Sep 4, 2017
@gfyoung
Copy link
Member

gfyoung commented Sep 4, 2017

@s-weigand : Don't forget a whatsnew for this PR!

@codecov
Copy link

codecov bot commented Sep 4, 2017

Codecov Report

Merging #17430 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17430      +/-   ##
==========================================
- Coverage   91.15%   91.14%   -0.02%     
==========================================
  Files         163      163              
  Lines       49581    49582       +1     
==========================================
- Hits        45198    45191       -7     
- Misses       4383     4391       +8
Flag Coverage Δ
#multiple 88.92% <100%> (ø) ⬆️
#single 40.25% <0%> (-0.07%) ⬇️
Impacted Files Coverage Δ
pandas/plotting/_core.py 82.69% <100%> (+0.01%) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.72% <0%> (-0.1%) ⬇️
pandas/core/indexes/datetimes.py 95.43% <0%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1981b67...13b60dc. Read the comment docs.

@codecov
Copy link

codecov bot commented Sep 4, 2017

Codecov Report

Merging #17430 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17430      +/-   ##
==========================================
- Coverage   91.15%   91.14%   -0.02%     
==========================================
  Files         163      163              
  Lines       49581    49582       +1     
==========================================
- Hits        45198    45191       -7     
- Misses       4383     4391       +8
Flag Coverage Δ
#multiple 88.92% <100%> (ø) ⬆️
#single 40.25% <0%> (-0.07%) ⬇️
Impacted Files Coverage Δ
pandas/plotting/_core.py 82.69% <100%> (+0.01%) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.72% <0%> (-0.1%) ⬇️
pandas/core/indexes/datetimes.py 95.43% <0%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1981b67...1ebfb13. Read the comment docs.

@pep8speaks
Copy link

pep8speaks commented Sep 4, 2017

Hello @s-weigand! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on September 05, 2017 at 17:38 Hours UTC

@jreback jreback added this to the 0.21.0 milestone Sep 4, 2017
@@ -342,7 +342,9 @@ def _compute_plot_data(self):
label = 'None'
data = data.to_frame(name=label)

numeric_data = data._convert(datetime=True)._get_numeric_data()
# GH16953
data = data._convert(datetime=True, timedelta=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need the _convert at all here. this is for object types that need to be coerced. almost all routines in pandas already assume this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The _convert is needed for a test, where a Series with numeric values and dtype object is used (tests.plotting.test_series.test_valid_object_plot).
If _convert isn't used, that test will fail in circleci.
To prevent the test from failing, _convert can either be used only on Series with dtype==object and after the to_frame (line 344), or on data in general after the Series specific part (line 346 as is now).

@@ -400,7 +400,7 @@ I/O
Plotting
^^^^^^^^
- Bug in plotting methods using ``secondary_y`` and ``fontsize`` not setting secondary axis font size (:issue:`12565`)

- Bug when plotting Timedelta and Datetime on y-axis (:issue:`16953`)
Copy link
Contributor

Choose a reason for hiding this comment

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

say timedelta and datetime dtypes (and use double back-ticks).

"timedelta": [pd.Timedelta(10, unit="s"),
pd.Timedelta(10, unit="m"),
pd.Timedelta(10, unit="h")],
"datetime": [pd.to_datetime("2017-08-01 00:00:00"),
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a datetime with a tz as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure ^^

Copy link
Contributor

Choose a reason for hiding this comment

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

I think some other types, e.g. Period might fail, if you can add them in another test with xfail would be fine (Categoricals might work as they area converted to ndarrays)

with pytest.raises(TypeError):
testdata.plot(y="text")

@pytest.mark.xfail
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to have a reason='not support for period, categorical, datetime_mixed_tz') here

numeric_data = data._convert(datetime=True)._get_numeric_data()
# GH16953, _convert is needed as fallback, for ``Series``
# with ``dtype == object``
data = data._convert(datetime=True, timedelta=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

did you take out the _convert and see, on what input does it fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yap i did, circleci fails at tests.plotting.test_series.test_valid_object_plot, since the Series is of dtype == object (s = Series(lrange(10), dtype=object), also see comment on your last requested changes).
Also i think doing a general data._convert is more sturdy, than just calling in if the Seriesis of type object (which would be the minimal change for the test to pass).

Copy link
Contributor

Choose a reason for hiding this comment

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

ok I guess leave it in is ok. In general we don't automatically coerce object types (even if they are numbers) in other functions / places; we happen to do it in plotting for compat I think. Can you open a new issue for this to discuss (deprecating this).

@jreback
Copy link
Contributor

jreback commented Sep 6, 2017

lgtm. @TomAugspurger

@TomAugspurger TomAugspurger merged commit 25d5299 into pandas-dev:master Sep 6, 2017
@TomAugspurger
Copy link
Contributor

Thanks @s-weigand!

@s-celles
Copy link
Contributor

s-celles commented Sep 6, 2017

@s-weigand What is matplotlib issue number? Thanks for this fix.

jbrockmendel pushed a commit to jbrockmendel/pandas that referenced this pull request 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
jowens pushed a commit to jowens/pandas that referenced this pull request 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
@s-weigand s-weigand deleted the fix_issue_#16953 branch October 15, 2017 19:44
alanbato pushed a commit to alanbato/pandas that referenced this pull request 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 pull request 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 this pull request may close these issues.

Plotting Timedelta on y-axis
6 participants