python - 在 Pandas 数据框中查找最近的日期

标签 python pandas

我有一个 csv 文件,我读入了 pandas 数据框。日期和时间列在“日期时间”列中。我想找到最近和最近的日期来创建索引以创建时间序列图。 pandas 有没有返回最近日期和最近日期的函数?

编辑:
我已经尝试过使用最小值和最大值。他们给出不正确的答案。

>>> f['Start Date']  
Trip ID  
4576       8/29/2013 14:13  
4607       8/29/2013 14:42  
4130       8/29/2013 10:16  
4251       8/29/2013 11:29  
4299       8/29/2013 12:02  
4927       8/29/2013 18:54  
4500       8/29/2013 13:25  
4563       8/29/2013 14:02  
4760       8/29/2013 17:01  
4258       8/29/2013 11:33  
4549       8/29/2013 13:52  
4498       8/29/2013 13:23  
4965       8/29/2013 19:32  
4557       8/29/2013 13:57  
4386       8/29/2013 12:31  
...  
198757     2/28/2014 20:40  
198760     2/28/2014 20:59  
198761     2/28/2014 20:59  
198763     2/28/2014 21:32  
198764     2/28/2014 21:32  
198765     2/28/2014 21:34  
198766     2/28/2014 21:41  
198767     2/28/2014 21:50  
198768     2/28/2014 21:54  
198770     2/28/2014 22:19  
198771     2/28/2014 22:15  
198772     2/28/2014 22:38  
198773     2/28/2014 22:45  
198774     2/28/2014 23:01  
198775     2/28/2014 23:20  
Name: Start Date, Length: 144015, dtype: object  
>>> min(f['Start Date'])  
'1/1/2014 0:14'  
>>> max(f['Start Date'])  
'9/9/2013 9:59'  

最佳答案

首先使用将您的日期列转换为日期时间列

>> df['StartDate'] = pd.to_datetime(df['StartDate'])
然后,您可以使用找到最早的日期和最近的日期
>> least_recent_date = df['StartDate'].min()
>> most_recent_date = df['StartDate'].max()

关于python - 在 Pandas 数据框中查找最近的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29886609/

相关文章:

python - Pandas 数据框 groupby 出现在两列中的文本值

python - 使用列表中的值替换列中的值

python - 限制 Django 中的 Markdown

python - 正则表达式根据缩写名的大写字母和句点组合进行拆分

python - 使用盈透证券 TWS API 将价格作为对象/变量返回 - Python

python - 为 mpld3 创建一个新插件

python - 2 天窗口的滚动总和

python - 错误的 numpy 平均值?

python - Numpy 舍入问题

python - 如果列匹配,则合并两个 csv 文件,然后覆盖