【发布时间】:2020-07-10 08:38:30
【问题描述】:
在如下的df中:
id timestamp temperature
27581 27822 2020-01-02 07:53:05.173 19.5
27582 27823 2020-01-02 07:53:05.273 20.0
27647 27888 2020-01-02 10:01:46.380 20.5
27648 27889 2020-01-02 10:01:46.480 21.0
27649 27890 2020-01-02 10:01:48.463 21.5
27650 27891 2020-01-02 10:01:48.563 22.0
27711 27952 2020-01-02 10:32:19.897 21.5
27712 27953 2020-01-02 10:32:19.997 21.0
27861 28102 2020-01-02 11:34:41.940 21.5
...
在生成情节的 for 循环中,我想在情节标题内打印 date 的工作日。 date 是一个 datetime.date 对象。但是我在格式化日期时遇到了一些错误。我根据this answer:
df['Date'] = df['timestamp'].dt.date
df.set_index(df['timestamp'], inplace=True)
for date in df['Date'].unique():
df_date = df[df['Date'] == date]
...
plt.title(date, date.strftime('%B')) # I want to print both the date and the corresponding weekday.
日期以2020-01-01 格式显示日期,这很好,但工作日部分返回错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-56-6cc9c07f6879> in <module>()
---> 86 plt.title(date, date.strftime('%B'))
87
88 number.append(np.count_nonzero(df2['events'][minLim:maxLim]))
2 frames
/usr/local/lib/python3.6/dist-packages/matplotlib/text.py in update(self, kwargs)
174 # Update bbox last, as it depends on font properties.
175 sentinel = object() # bbox can be None, so use another sentinel.
--> 176 bbox = kwargs.pop("bbox", sentinel)
177 super().update(kwargs)
178 if bbox is not sentinel:
AttributeError: 'str' object has no attribute 'pop'
然后我尝试了这个,基于this answer:
df['Date'] = df['timestamp'].dt.date
df.set_index(df['timestamp'], inplace=True)
for date in df['Date'].unique():
df_date = df[df['Date'] == date]
...
year, month, day = (int(x) for x in date.split('-'))
answer = datetime.date(year, month, day).weekday()
plt.title(date, answer)
返回的
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-57-03b7529a410a> in <module>()
82 number = []
83 ax.autoscale()
---> 84 year, month, day = (int(x) for x in date.split('-'))
85 answer = datetime.date(year, month, day).weekday()
86 plt.title(date, answer)
AttributeError: 'datetime.date' object has no attribute 'split'
更新:
我尝试使用以下方法在日期框架中为每个唯一的“日期”创建“工作日”列:
for date in df['Date'].unique():
df_date = df[df['Date'] == date]
df_date['Date'] = pd.to_datetime(df_date['Date'], errors='coerce')
df_date['Weekday'] = df_date['Date'].dt.dayofweek #Add 'Weekday' column.
print(df_date)
返回警告:
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:7: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
尽管已打印警告数据框df_date。但是我应该如何让它从循环中返回both date 和weekday(例如"2020-04-02, Thursday")?
我应该使用这样的东西吗:
weekday = df_date.loc[date, df_date['Weekday']]
要在循环中获取date 对应的工作日?
【问题讨论】:
标签: python pandas numpy datetime for-loop