-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
VIS: DataFrame.plot drops datetime data when kind is scatter #8113
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
Quick update: this will take a bit of work. @jreback what's the current thinking on adding new dtypes for Once I have that I can use the |
can u give an example of what that would return? u can certainly interpret date/time just not sure what that would mean here |
adding a date and/or time dtype would be pretty tricky and not sure a lot of benefit for it |
FYI you can infer an object dtype |
For a line plot in won't make much sense, but I think a scatter plot would work. That way you can see the how In [25]: df = tm.makeTimeDataFrame().reset_index().rename(columns={'index': 'datetime'})
In [26]: df['day'] = df.datetime.dt.day
In [27]: df.plot(x='day', y='A', kind='scatter')
Out[27]: <matplotlib.axes._subplots.AxesSubplot at 0x1181ca550> Since |
you could easily make a DatetimeIndex method to return what u need (like date/time) |
The problem now is that the plotting code calls |
ahah maybe add a way to include it then? or some method of coercing ? |
I'm going to close this for now. matplotlib is able to handle it if everything on the axis is the same type ( |
It might be helpful documentation to add a comment to this (closed) issue indicating how to do this in matplotlib (plot_date ?). There is still a bug in both pandas and matplotlib, I think, that a datetime.time against datetime.time scatter plot does not work. If that's a different issue, perhaps linking them would be good. (This issue is the closest I've found.) |
@JeffAbrahamson By any chance did you find a way to plot time vs time scatterplots or histograms? (I tried, but failed, and Google brought me here and to this SO question.) Of course, other than doing it manually or sticking to seconds since epoch. |
I did not figure it out. My use case had smallish times (I was visualizing members of split times in a race) and so I eventually plotted integer seconds against integer seconds (numbers were all in the range from 450 to1350). |
Hi @TomAugspurger and @jreback! I think it might be worth re-opening this issue; @jaclynweiser (with a few others) and I have been surprised recently by things like this: from datetime import datetime
import pandas as pd
df = pd.DataFrame({'x': [datetime.now() for _ in range(10)], 'y': range(10)})
df.plot(x='x', y='y', kind='scatter') This gives Interestingly, you do get a plot with just What do you think? Is there some a good work-around for this? If so, what? It's surprising to me that a datetime scatterplot isn't possible with pandas. |
Agreed that it's surprising. Right now time series plots (datetime x axis) are completely separate from everything else. I've had refactoring all that to integrate with all our other plotting code on my todo list for a while. Best workaround right now is probably df.plot(x=x, y=y, style=".") |
Thanks @TomAugspurger! |
Yup, found this rather surprising as well, not to be able to scatterplot a datetime object against a numeric object. (If I've followed the thread correctly, I should be okay with a Isn't (Also, apologies if I'm off the mark here, am relatively new to pandas. Thanks for considering). |
Any update on this? As of 0.18, pandas still gives a key error when plot is called with |
@colin-svds this is a closed issue (from quite a while ago). you can open a new one if you would like. but pls read this one for work-arounds. I don't know if @TomAugspurger has anything more. |
Nope I haven't ever made progress on it. I've reopened it for now if anyone wants to take a shot. It might be as simple as reworking the plotting methods to use something other than |
Any solution for this as if now @TomAugspurger @jreback ? |
It's still open. Let us know if you're interested in working on it. |
I think this issue can be closed now. @TomAugspurger @jreback Regarding:
Since Tom tested it long ago (issue was made 5 years ago), and matplotlib has changed the behaviour, now the situation is opposite: matplotlib is ok with And if Although x axis looks different, this is due to pandas has its own datetime formatter, so slightly different than matplotlib's formatter. but this is a different issue to me. |
@charlesdong1991 can u add a test for the above |
@jreback I think the test I used in #30434 is almost identical to this one except for the dates = pd.date_range(start=date(2019, 1, 1), periods=12, freq="W")
vals = np.random.normal(0, 1, len(dates))
df = pd.DataFrame({"dates": dates, "vals": vals}) The reason I initially xref (other than closes) this issue was the values of example above was Do you still want to have the same test for this? I could add one if you prefer this way, though a bit duplicated compared to the existing one. |
can u add the test and assert that error; that would be enough to close this i think |
This works fine:
This raises a
KeyError
We call
df._get_numeric_data()
which excludes datetimes.May happen for other kinds too.
I'll fix this; just have to decide how much refactoring.
Matplotlib is ok with
datetime.time
values, it chokes ondatetime
values.The text was updated successfully, but these errors were encountered: