如何获取datetime.date对象的工作日 [英] How to get weekday of datetime.date object
问题描述
在如下所示的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对象.但是格式化日期时出现了一些错误.我根据此答案尝试了类似的操作:
In a for-loop that generate plot, I want to print the weekday of date
inside the plot title. date
is a datetime.date object. But I incurred some error when formatting the date. I tried something like this, based on 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
格式显示日期,可以,但是工作日部分返回错误:
The date displays date in the format 2020-01-01
which is fine, but the weekday section returned error:
---------------------------------------------------------------------------
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'
然后我基于此答案:
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'
更新:
Update:
我尝试使用以下方法在日期框架中为每个唯一的日期"创建工作日"列:
I tried to create the "weekday" column in the date frame for each unique 'date' using:
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
.但是我应该如何使其从循环中返回 和date
以及weekday
(例如,"2020-04-02, Thursday"
)呢?
despite the warnings dataframe df_date
had been printed.But how should I make it return both date
and weekday
from the loop(for example "2020-04-02, Thursday"
)?
我应该使用这样的东西吗?
Should I use something like this:
weekday = df_date.loc[date, df_date['Weekday']]
在循环中获取相应的工作日date
?
to get the corresponding weekday of date
in the loop?
推荐答案
Use the weekday property of a DatetimeIndex, link. See the example at the bottom of the linked page.
示例
import pandas as pd
import matplotlib.pyplot as plt
data = [
["2020-01-02 10:01:48.563", "22.0"],
["2020-01-02 10:32:19.897", "21.5"],
["2020-01-02 10:32:19.997", "21.0"],
["2020-01-02 11:34:41.940", "21.5"],
]
df = pd.DataFrame(data)
df.columns = ["date", "temp"]
df["date"] = pd.to_datetime(df["date"])
df["weekday"] = df["date"].dt.weekday
df["day_name"] = df["date"].dt.day_name()
print(df)
for day_name in df["day_name"].unique():
plt.figure()
plt.plot(df["date"], df["temp"])
plt.title(day_name)
plt.show()
给出
date temp weekday day_name
0 2020-01-02 10:01:48.563 22.0 3 Thursday
1 2020-01-02 10:32:19.897 21.5 3 Thursday
2 2020-01-02 10:32:19.997 21.0 3 Thursday
3 2020-01-02 11:34:41.940 21.5 3 Thursday
和情节
根据评论更新04/04
Update 04/04 in response to comment
import pandas as pd
import matplotlib.pyplot as plt
data = [
["2020-01-02 10:01:48.563", "22.0"],
["2020-01-02 10:32:19.897", "21.5"],
["2020-01-02 10:32:19.997", "21.0"],
["2020-01-02 11:34:41.940", "21.5"],
]
df = pd.DataFrame(data)
df.columns = ["timestamp", "temp"]
df["timestamp"] = pd.to_datetime(df["timestamp"])
df['Date'] = df['timestamp'].dt.date
df.set_index(df['timestamp'], inplace=True)
df['Weekday'] = df.index.day_name()
for date in df['Date'].unique():
df_date = df[df['Date'] == date]
plt.figure()
plt.plot(df_date["timestamp"], df["temp"])
plt.title("{}, {}".format(date, df_date["Weekday"].iloc[0]))
plt.show()
请注意,这将为每个唯一的日期生成一个图,我想这就是您想要的.
Note this will produce a plot for each unique date, I assume thats what you are after.
对于上面的有限数据示例,这将产生图
For the limited data example above, this produces the plot
这篇关于如何获取datetime.date对象的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!