可以显示每月大熊猫的最小日期和最大日期的循环
datetimedata的映像样本...它落后于4/1/2022
import pandas as pd
import calendar
from datetime import date
sd = '2021-12-01'
ed = '2022-03-31'
date_range = pd.date_range(sd,ed,
freq='MS').map(lambda x: (x.year, x.month)).tolist()
def get_dates(year, month):
return (date(year, month, 1).strftime("%d-%b-%Y"),
date(year,
month,
calendar.monthrange(year, month)[1]
).strftime("%d-%b-%Y"))
[get_dates(year, month)
for year, month in date_range]
df2 = pd.read_csv('datetimedata.csv')
df2.loc[:,'timestamp'] = pd.to_datetime(df2.loc[:,'timestamp'])
df2 = df2.loc[(df2['timestamp'] >= sd)
& (df2['timestamp'] <= ed)]
要在我的控制台中获取最小值和最大值,它在下面给出了此答案:
df2.timestamp.min()
Out[102]: Timestamp('2021-12-01 00:00:00')
df2.timestamp.max()
Out[103]: Timestamp('2022-03-31 00:00:00')
但是我想要它,以便我要在第一次运行后停止循环(2021-12-31 00:00:00)。然后,当我将循环通过3月,然后运行代码,最大值应为2022-03-31,最小值应为2022-03-01。我只想希望它连续每个月循环,显示每个月的最小日期和最大日期。
Image sample of the datetimedata... It goes down to 4/1/2022
import pandas as pd
import calendar
from datetime import date
sd = '2021-12-01'
ed = '2022-03-31'
date_range = pd.date_range(sd,ed,
freq='MS').map(lambda x: (x.year, x.month)).tolist()
def get_dates(year, month):
return (date(year, month, 1).strftime("%d-%b-%Y"),
date(year,
month,
calendar.monthrange(year, month)[1]
).strftime("%d-%b-%Y"))
[get_dates(year, month)
for year, month in date_range]
df2 = pd.read_csv('datetimedata.csv')
df2.loc[:,'timestamp'] = pd.to_datetime(df2.loc[:,'timestamp'])
df2 = df2.loc[(df2['timestamp'] >= sd)
& (df2['timestamp'] <= ed)]
When I write to get the min and max in my console it gives this answer below:
df2.timestamp.min()
Out[102]: Timestamp('2021-12-01 00:00:00')
df2.timestamp.max()
Out[103]: Timestamp('2022-03-31 00:00:00')
However I wanted it so that if I were to stop the loop after the first run the max would be (2021-12-31 00:00:00). Then when I have the loop going through March then the codes finishes running, max should be 2022-03-31 and min should be 2022-03-01. I just want it to loop through each month showing the min and max date for each consecutive month.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您为什么完全需要
date_range
。为什么不简单地按月按df2 ['TIMESTAMP']
进行分组?现在:
附录
您真的想拥有一个经过几个月的明确循环,也可以这样做:
根据评论中的请求进行编辑
,以将数据截断到特定的时间窗口:
I'm not sure why you need
date_range
at all. Why not simply grouping by month ofdf2['timestamp']
?Now:
Addendum
If you really want to have an explicit loop that goes through the months, you can do so as well:
Edit
As per the request in the comment, in order to truncate the data to a specific time window: