-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
problems with timestamps in matplotlib seems related to bug #9779 #11649
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
Comments
You'll get more help w/ this if you minimize it and get rid of extra stuff like |
#9779 is about the A code for reproduction seems to be the following:
This fails with the same error The solution (for now) is to use the numpy array provided by
works as expected. |
But I totally agree that it is somehow confusing that
works fine, while
does not. Essentially the same issue, but for scatter, is raised in #11391. |
Okay....
print('pre x', x, type(x), type(x[0]))
if x.ndim == 1:
x = x[:, np.newaxis]
print('x after', x, type(x), type(x[0, 0])) and this converts x from a pandas time series to a numpy datetime64 array:
So for For So, not sure what the right thing to do here is. We could easily pre-process |
Checked blame, and the numpy typecast came into |
Wow it's kind of depressing to see what I always considered as a great feature turning out to be pure coincidence. In any case, this "feature" is used a lot in the wild, so even though I somehow agree that the conversion may be wrong in fact, I would argue that one should not break many people's codes by removing it. In general, I could imagine that adding a couple of pandas examples to the matplotlib gallery would be really helpful; but the problem is that due to pandas not being a dependency, those couldn't be run through sphinx-gallery, producing any output figures. |
Thanks, The solution proposed by Elan Ernest indeed converted dates correctly but as a side effect I got got weird behavior with the bars. Before (not using .values, not doing However this seems to be a problem out of the scope of this topic. Does the workaround proposed by Elan Ernest deserves an entry in https://matplotlib.org/gallery/recipes/common_date_problems.html? I was trying to find a solution for this problem there before opening this issue Thank you very much again |
Anyway I would appreciate if someone has a tip for solving both problems as I used to have this problem before and this was one of the reasons I stopped using values :) |
@ImportanceOfBeingErnest - I guess I think the behaviour of If that is fixed, then maybe we can talk about making sure Getting this right should all be thought about as part of the units MEP. ping @dopplershift @tacaswell |
@danieljmr you are using pandas... As you can probably tell, pandas is not a native part of matplotlib; its a little hard for us to specify "common problems" for all the downstream packages (though of course we can/should try and smooth out any problems if we can help on our end). |
Pandas and tabular data is a big use case for our users. Even though we do not directly depend on pandas, we need to make sure we work well with it. Pandas is defiantly a special case of our down-stream consumers.
I do not understand the details of this bug report, but in general I agree. There is a difference between the "official public" API and the "what users rely on so it is defacto public" API which is one of the biggest challenges of long-term maintenance of Matplotlib. |
I agree it’d be nice for us to work well w pandas; I guess the question is how much do we do natively and how much do we ask them to do. On this bug, which is pretty long standing from what I can tell, I think the ball is in pandas court because it is their converter that isn’t converting. That plot works is because we never give their converter the chance to work in plot. |
This needs to be a collaborative effort with pandas (users don't care whos problem it is, they just want it to work!). I have added this to the to-do list for the scipy sprints. Hopefully we can got a mpl dev and a pandas dev to sit together and sort out where this is going sideways. attn @TomAugspurger |
I won't be available during sprints, but if people need pandas questions answered they can post here and I'll try to answer when I'm back online. |
@danieljmr To solve the remaining problem of your bars being too wide, you need to set a useful width for them. The units of matplotlib datetime plots are days, so the width needs to be set in fractions of a day. E.g. if you wanted to have hourly wide bars, you'd set @TomAugspurger There isn't really any question to be answered here; it's more that it needs to be discussed where matplotlib and pandas can meet when it comes to datetime plotting.
even with datetimes involved. |
Gentlemen, At this point, having all my questions answered, I will proceed with the closure. Great support! |
This still identifies a bug that we need to talk about. |
Note that if we deregister the pandas converters this works fine: import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
import pandas as pd
pd.plotting.deregister_matplotlib_converters()
dates = [datetime(2018,7,i) for i in range(1, 5)]
values = np.cumsum(np.random.rand(len(dates)))
df = pd.DataFrame({"dates":dates, "values" : values})
plt.plot(df["dates"], df["values"])
plt.scatter(df["dates"], df["values"])
plt.show() |
Sorry, I will close this in favour of #11391 where the use cross section is more complete.... |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug summary
I am gettin the following error when trying to plot dates in X-axis. It seems that the same bug was reported in #9779 bug as I already have the fix I am wondering why I am still getting the error. Maybe something I am doing wrong?
The error is:
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')
input data is a csv with format:
If TIMESTAMP is not converted to np.datenum64 the X-axis will be converted to numbers but it will dispay the graph ok
Code for reproduction
data['TIMESTAMP'] = pd.to_datetime(data['TIMESTAMP'])
bar = axes.bar(data['TIMESTAMP'], data[a], bottom=margin_bottom, label=a, color=palet[a], width=0.03)
margin_bottom += data[a]
Actual outcome
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('float64')
Expected outcome
X-axis showing dates. Dates can be plotted when using pandas directly but I need to perform customization like fixed colors for data types plotted as bars and sequence of bar stack
Matplotlib version
INSTALLED VERSIONS
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: English_United States.1252
pandas: 0.22.0
pytest: None
pip: 10.0.1
setuptools: 39.0.1
Cython: None
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
pip
The text was updated successfully, but these errors were encountered: