-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Use data argument for scatter plotting timestamps from pandas #11391
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
Fully agree, some consitency would be nice |
This seems to be a bug in that the data isn’t being converted to the right form early enough. |
I actually think this is likely downstream in pandas. The units converter called is I'm going to close as needing a downstream fix, but I may be just passing the buck and not understanding something. |
Yep, I guess the problem is more that |
What is the type that is failing to be promoted? I suspect that this is going to boil down to a datetime64 issue... |
Again, if you call As far as I can tell, the DatetimeConverter doesn't do any conversion on the Pandas series object: Adding the following print statements to ret = self.converter.convert(x, self.units, self)
print(self.converter)
print('x', x)
print('\n x type', type(x))
print('\nret', ret)
print('\nret type', type(ret))
As you can see, the Pandas unit conversion doesn't do anything to the series. |
Just to re-iterate the idea this is in panda's court; if we deregister the pandas converter this all works fine due to #10638. We did #10638 so that pandas didn't have to register their converter by default. I think a todo is to run a bunch of tests.... ping @TomAugspurger. 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() |
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.plot("dates", "values", data=df)
plt.scatter(x="dates", y="values", data=df)
plt.show() all works... |
Sorry for the delay. Yeah, this looks to be a pandas issue diff --git a/pandas/plotting/_converter.py b/pandas/plotting/_converter.py
index beebf84b8..cc835cd95 100644
--- a/pandas/plotting/_converter.py
+++ b/pandas/plotting/_converter.py
@@ -320,7 +320,11 @@ class DatetimeConverter(dates.DateConverter):
return values
elif isinstance(values, compat.string_types):
return try_parse(values)
- elif isinstance(values, (list, tuple, np.ndarray, Index)):
+ elif isinstance(values, (list, tuple, np.ndarray, Index, ABCSeries)):
+ if isinstance(values, ABCSeries):
+ # https://github.com/matplotlib/matplotlib/issues/11391
+ # Series was skipped. Convert to DatetimeIndex to get asi8
+ values = Index(values)
if isinstance(values, Index):
values = values.values
if not isinstance(values, np.ndarray): I'll put that up as a pandas PR. |
@TomAugspurger: What is panda's long-term plan vis-a-vis registering the matplotlib converter when pandas is imported? I'm going to close this as fixed on panda's end.... |
Long-term, we'd like to not have Right now we're supposed to have a warning if people are implicitly relying on our converters, but that's apparently not working... |
Thanks!, Ideally, I think matplotlib should be able to natively unpack panda's frames w/o pandas' converters, and I have such a PR submitted (#11664), but I wanted to make sure this was still pandas' long-term goal before pushing that PR forward. (i.e. there is not much point in making things work with the pandas' converters deregistered, if they are always going to be registered by |
It seems this has now become Schrödinger's converter.
or
makes this work. It will throw a warning if you do neither of the above. Is this a semantic problem? I guess one wouldn't expect to get the correct output by performing a registration or a deregistration, while leaving it in a transient state if doing neither? |
Last I checked, importing pandas still registers their converter. If you unregister it, you are using matplotlibs converter. |
Bug report
Bug summary
This may be either a bug report or a feature request, depending how you view things. The issue is that you cannot use the
data
argument toscatter
to plot a pandas dataframeTimestamp
column.Code for reproduction
Consider the following data
Using the
data
argument, you may plotYou may equally plot
You may also scatter
However, you may not scatter the timestamps
This fails with a
TypeError: invalid type promotion
This is a bit surprising since you may well plot timestamps through scatter
just not through the
data
argument.Expected outcome
Given that one may plot the timestamps correctly with
plot
and timestamps withscatter
by providing the values, it would be nice to be able to use thedata
argument withscatter
as well.... and ideally with
bar
,fill_between
etc. ;-)Matplotlib version
The text was updated successfully, but these errors were encountered: