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

DOC: better argcheck bar #13162

Merged
merged 1 commit into from Jan 17, 2019
Merged

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Jan 11, 2019

PR Summary

Closes #13142

This checks that x and width in bar can be subtracted and emits a more useful message than before.

The underlying issue is that width=0.8, which has no units, and in this case x has datetime64 units.

Code

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime

def timestamp_to_datetime(raw):
    return datetime.fromtimestamp(raw/1000)

df = pd.DataFrame([{
    'timestamp': 1500854400000,
    'activity': 5},
{
    'timestamp': 1500940800000,
    'activity': 6
}])
df['date'] = df['timestamp'].apply(timestamp_to_datetime)

plt.figure(figsize=(20,10))
plt.bar(df['date'], df['activity'])
plt.show();

Before:

Traceback (most recent call last):
  File "testBarDT.py", line 24, in <module>
    plt.bar(df['date'], df['activity'])
  File "/Users/jklymak/matplotlib/lib/matplotlib/pyplot.py", line 2438, in bar
    **({"data": data} if data is not None else {}), **kwargs)
  File "/Users/jklymak/matplotlib/lib/matplotlib/__init__.py", line 1778, in inner
    return func(ax, *args, **kwargs)
  File "/Users/jklymak/matplotlib/lib/matplotlib/axes/_axes.py", line 2277, in bar
    left = x - width / 2
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')

After

Traceback (most recent call last):
  File "/Users/jklymak/matplotlib/lib/matplotlib/axes/_axes.py", line 2278, in bar
    left = x - width / 2
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "testBarDT.py", line 18, in <module>
    plt.bar(df['date'], df['activity'])
  File "/Users/jklymak/matplotlib/lib/matplotlib/pyplot.py", line 2438, in bar
    **({"data": data} if data is not None else {}), **kwargs)
  File "/Users/jklymak/matplotlib/lib/matplotlib/__init__.py", line 1778, in inner
    return func(ax, *args, **kwargs)
  File "/Users/jklymak/matplotlib/lib/matplotlib/axes/_axes.py", line 2282, in bar
    f'({width.dtype}) are incompatible') from e
ValueError: the dtypes of parameters x (datetime64[ns]) and width (float64) are incompatible

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak jklymak force-pushed the doc-better-argcheck-bar branch 2 times, most recently from b052a72 to 5eaacbf Compare January 11, 2019 18:45
@jklymak jklymak force-pushed the doc-better-argcheck-bar branch 2 times, most recently from c6efd9f to f2fca36 Compare January 11, 2019 21:38
@NelleV
Copy link
Member

NelleV commented Jan 16, 2019

I've fixed the pep8 errors directly. I'll merge once test pass!

Thanks @jklymak !

@jklymak
Copy link
Member Author

jklymak commented Jan 16, 2019

Ooops, sorry missed fixing those. Feel free to squash merge, or I can self-merge when CI passes.

@dstansby
Copy link
Member

Just restarted the tests since they're fixed, anyone feel free to merge if/when they pass!

@dstansby dstansby added this to the v3.1 milestone Jan 17, 2019
@jklymak jklymak merged commit c9ed617 into matplotlib:master Jan 17, 2019
@jklymak jklymak deleted the doc-better-argcheck-bar branch January 17, 2019 19:33
@NelleV
Copy link
Member

NelleV commented Jan 17, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants