I know this question is really close to a lot of other answered questions, but all previous answers are giving me the same traceback issues.
I have a straightforward time series, and I'm trying to highlight the maximum point. I'm running into problems manipulating a Pandas Dataframe to obtain the maximum y value for plotting on a graph. I think I'm nearly there, but I think the parse_dates parameter of the pd.read_csv import is messing with my indexing.
When I import the dataset, I have a datetime column, and a wind_speed column. When I resample for the daily average, the title for the variable column disappears and the datetime column becomes uncallable.
Before taking the daily average:
In[12]: weather.head()
Out[12]: wind_speed
d_stamp_t_stamp
2017-07-26 00:05:09 1.31
2017-07-26 00:35:13 1.62
2017-07-26 01:05:05 1.50
.......
After taking the daily average:
wind_avg = weather.wind_speed.resample('D').mean()
d_stamp_t_stamp
2017-09-01 3.870625
2017-09-02 4.386875
2017-09-03 5.426739
2017-09-04 2.718750
2017-09-05 3.407708
The label for the wind_speed column goes away, and I can't seem to sample that data anymore.
So this is the code for the time series I have so far:
## Import weather data.
weather = pd.read_csv('/Users/regina/university_projects/Themo_Data/Weather0717-0618.csv',
parse_dates=[[0,1]], index_col=0)
wind_avg = weather.wind_speed.resample('D').mean()
## Wind Speed graph
windplot = wind_avg.plot(title="Wind Speed", figsize=(12,8),
fontsize=12, marker='o', markersize=7)
windplot.set_xlabel("Date"),windplot.set_ylabel("Wind Speed in m/s")
Which gives me this graph with wind speed average on the y axis.
The problem comes when I try to annotate the maximum wind speed.
y0 = max(wind_avg.wind_speed)
xpos = wind_avg.wind_speed.index(y0)
x0 = (wind_avg.d_stamp_t_stamp[xpos])
windplot.annotate(
"Max Speed", xy=(x0,y0), ha='right',
va='bottom', textcoords='offset points', bbox=dict(BoxStyle='Round, pad=0.5', fc='yellow',
alpha=0.5), arrowprops=dict(facecolor='black', shrink=0.05))
I get an attribute error message like this:
Traceback (most recent call last):
File "<ipython-input-15-5e45876c5ebc>", line 5, in <module>
y0 = max(wind_avg.wind_speed)
File "/Users/regina/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 4372, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'wind_speed'
Is there something about the way I'm resampling the wind_speed column that removes its label? Thank you all so much!
In the line
wind_avg = weather.wind_speed.resample('D').mean()
you apply resample
to the single Pandas Series which is in the column wind_speed
of your Dataframe, so you'll get a Series as return value:
type(wind_avg)
Out: pandas.core.series.Series
Try
weather_avg = weather.resample('D').mean()
type(weather_avg)
Out: pandas.core.frame.DataFrame
and you'll get your whole weather dataset resampled per days.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.