Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

arrays - np.vstack() with datetime and float data types

I have a Pandas dataframe with datetime elements ('date') and floats ('cups) and am wondering if there's anyway to use np.vstack() to stack the two.

Previously, I had converted the datetime elements to floats and used vstack() without issue. However, I am using xx to update a scatterplot with a slider and, since I would like the plot to use dates, can no longer use this method.

xx = np.vstack((df['date'], df['cups']))
scat.set_offsets(xx.T)

I see that there's been a similar question, but I'm not sure how to adapt the answer given there to my situation: Can ndarray store datetime and float?.

Since the end-goal is to call set_offsets with the right kind of structure, the solution need not use vstack. I'm just not sure how to get the structure set_offsets needs without using vstack.

question from:https://stackoverflow.com/questions/66055875/np-vstack-with-datetime-and-float-data-types

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solved! Thanks to @hpaulj for the pointer. I was wrongly assuming that scat.set_offsets() needed datetime input for a plot with dates, so was resistant to passing floats. It turns out, however, that a date scatter plot can accept floats passed to set_offsets and still display the data correctly.

I've converted the column in question via the following:

df.loc[:, 'date'] = df['date'].map(lambda x: matplotlib.dates.date2num(x)).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...