Python 从列表中确定最晚日期

Python 从列表中确定最晚日期,python,datetime,pandas,Python,Datetime,Pandas,我正试图从存储在数据框中的日期列表中确定最新日期。问题是我不知道如何使用pandas比较datetime元素列表 基本上,我正在尝试做以下工作 给定此数据集,日期(或类似的内容) 日期 0 1985-4-5 1 1985-2-9 2 1983-2-2 3 1998-1-12 n=0 而(n如果您的Date列是dtypedatetime64[ns],则可以使用max方法: In [35]: df = pd.DataFrame({'Date':pd.to_datetime(['1985-4-

我正试图从存储在数据框中的日期列表中确定最新日期。问题是我不知道如何使用pandas比较datetime元素列表

基本上,我正在尝试做以下工作

给定此数据集,
日期
(或类似的内容)

日期
0 1985-4-5
1 1985-2-9
2 1983-2-2
3 1998-1-12    
n=0

而(n如果您的
Date
列是dtype
datetime64[ns]
,则可以使用
max
方法:

In [35]: df = pd.DataFrame({'Date':pd.to_datetime(['1985-4-5', '1985-2-9', '1983-2-2', '1998-1-12'])})
In [44]: df
Out[44]: 
        Date
0 1985-04-05
1 1985-02-09
2 1983-02-02
3 1998-01-12

[4 rows x 1 columns]

In [45]: latest = df['Date'].max()

In [46]: print(latest)
1998-01-12 00:00:00
In [51]: df['Date'][:4].max()
Out[51]: Timestamp('1998-01-12 00:00:00')

如果
Date
列由字符串组成,则首先将它们转换为
datetime64[ns]
对象:

In [47]: df = pd.DataFrame({'Date':['1985-4-5', '1985-2-9', '1983-2-2', '1998-1-12']})

In [48]: df['Date'] = pd.to_datetime(df['Date'])

In [49]: df['Date'].max()
Out[49]: Timestamp('1998-01-12 00:00:00')

如果只想确定前4个日期中的最大值,则可以在调用
max
方法之前对序列进行切片:

In [35]: df = pd.DataFrame({'Date':pd.to_datetime(['1985-4-5', '1985-2-9', '1983-2-2', '1998-1-12'])})
In [44]: df
Out[44]: 
        Date
0 1985-04-05
1 1985-02-09
2 1983-02-02
3 1998-01-12

[4 rows x 1 columns]

In [45]: latest = df['Date'].max()

In [46]: print(latest)
1998-01-12 00:00:00
In [51]: df['Date'][:4].max()
Out[51]: Timestamp('1998-01-12 00:00:00')

如果
Date
列的数据类型为
datetime64[ns]
,则可以使用
max
方法:

In [35]: df = pd.DataFrame({'Date':pd.to_datetime(['1985-4-5', '1985-2-9', '1983-2-2', '1998-1-12'])})
In [44]: df
Out[44]: 
        Date
0 1985-04-05
1 1985-02-09
2 1983-02-02
3 1998-01-12

[4 rows x 1 columns]

In [45]: latest = df['Date'].max()

In [46]: print(latest)
1998-01-12 00:00:00
In [51]: df['Date'][:4].max()
Out[51]: Timestamp('1998-01-12 00:00:00')

如果
Date
列由字符串组成,则首先将它们转换为
datetime64[ns]
对象:

In [47]: df = pd.DataFrame({'Date':['1985-4-5', '1985-2-9', '1983-2-2', '1998-1-12']})

In [48]: df['Date'] = pd.to_datetime(df['Date'])

In [49]: df['Date'].max()
Out[49]: Timestamp('1998-01-12 00:00:00')

如果只想确定前4个日期中的最大值,则可以在调用
max
方法之前对序列进行切片:

In [35]: df = pd.DataFrame({'Date':pd.to_datetime(['1985-4-5', '1985-2-9', '1983-2-2', '1998-1-12'])})
In [44]: df
Out[44]: 
        Date
0 1985-04-05
1 1985-02-09
2 1983-02-02
3 1998-01-12

[4 rows x 1 columns]

In [45]: latest = df['Date'].max()

In [46]: print(latest)
1998-01-12 00:00:00
In [51]: df['Date'][:4].max()
Out[51]: Timestamp('1998-01-12 00:00:00')
您可以使用带有reverse=true和
key
参数的模块和函数将字符串转换为datetime对象,在零索引处,您将得到以下代码的最新日期检查(阅读注释):

您还可以将函数用作:

>>> max(dates, key= lambda d: datetime.strptime(d, '%Y-%m-%d'))
'1998-1-12'
>>> min(dates, key= lambda d: datetime.strptime(d, '%Y-%m-%d'))
'1983-2-2'
选中此项可返回与日期字符串相对应的日期时间,并根据格式进行分析。

您可以使用模块和函数,使用reverse=true和
参数将字符串转换为日期时间对象,在零索引处,您将获得以下代码的最新日期检查(阅读注释):

您还可以将函数用作:

>>> max(dates, key= lambda d: datetime.strptime(d, '%Y-%m-%d'))
'1998-1-12'
>>> min(dates, key= lambda d: datetime.strptime(d, '%Y-%m-%d'))
'1983-2-2'
选中此项可返回与date_字符串相对应的datetime,并根据格式进行解析