首页 > 解决方案 > 如何从数据框中的日期时间列中查找日期范围?

问题描述

想知道如何在数据框中打印日期范围。似乎这很简单,但我无法在任何地方找到答案。有没有一种简单的方法可以使用 pandas 日期时间模块来做到这一点?

因此,如果这是数据框的一个小版本,例如:

日期 ID 价值
2020-09-23 14:00:00 4752764 12212
2020-10-25 08:00:00 4752764 12298
2020-10-28 12:00:00 4752764 12291
2020-10-29 18:00:00 4752764 12295

我怎么能得到这样的输出:

date_range = 2020-09-23 to 2020-10-29

或者

date_range = 23rd of September, 2020 to 29th of October, 2020

我很感激任何答案:)

标签: pythonpandasdataframedatetime

解决方案


尝试这个

df['Date'] = pd.to_datetime(df['Date']) # If your Date column is of the type object otherwise skip this
date_range = str(df['Date'].dt.date.min()) + ' to ' +str(df['Date'].dt.date.max())

推荐阅读