Pandas 日期时间和Pandas Timestamp对象之间的转换

Pandas 日期时间和Pandas Timestamp对象之间的转换

在本文中,我们将介绍如何在Pandas中进行日期时间和Pandas Timestamp对象之间的转换。Pandas是一个用于数据操作和分析的Python库,具有处理时间序列数据的强大功能。Pandas中的两种主要时间对象是datetime和Pandas Timestamp对象。

阅读更多:Pandas 教程

Pandas Timestamp对象

Pandas Timestamp对象是一个表示时间戳的对象,可以以纳秒精度表示任意日期和时间。可以使用Pandas提供的to_datetime()函数将字符串转换为Timestamp对象。

import pandas as pd

date_string = "2022-03-14"
date_timestamp = pd.to_datetime(date_string)
print(date_timestamp)
Python

这将输出Timestamp对象,表示2022年3月14日。

2022-03-14 00:00:00
Python

可以使用strftime()函数将Timestamp对象格式化为不同的字符串格式。

import pandas as pd

date_string = "2022-03-14"
date_timestamp = pd.to_datetime(date_string)

formatted_date = date_timestamp.strftime("%d-%m-%Y")
print(formatted_date)
Python

这将输出格式化后的日期字符串。

14-03-2022
Python

datetime对象

Python内置的datetime库提供了日期和时间对象。可以使用Pandas的Timestamp对象构造datetime对象。可以使用datetime()函数将Pandas Timestamp对象转换为datetime对象。

import pandas as pd
from datetime import datetime

date_string = "2022-03-14"
date_timestamp = pd.to_datetime(date_string)

date_datetime = datetime(date_timestamp.year, date_timestamp.month, date_timestamp.day)
print(date_datetime)
Python

这将输出datetime对象,表示2022年3月14日。

2022-03-14 00:00:00
Python

可以使用strftime()函数将datetime对象格式化为不同的字符串格式。

import pandas as pd
from datetime import datetime

date_string = "2022-03-14"
date_timestamp = pd.to_datetime(date_string)

date_datetime = datetime(date_timestamp.year, date_timestamp.month, date_timestamp.day)

formatted_date = date_datetime.strftime("%d-%m-%Y")
print(formatted_date)
Python

这将输出格式化后的日期字符串。

14-03-2022
Python

时间区间

Pandas还提供了TimeDelta和TimePeriod对象,用于表示时间区间。TimeDelta对象表示一段时间(例如,2天),而TimePeriod对象表示一个时间段,例如一个月或一年。

可以使用Pandas提供的to_timedelta()函数将字符串转换为TimeDelta对象。

import pandas as pd

delta_string = "2 days"
delta_timedelta = pd.to_timedelta(delta_string)
print(delta_timedelta)
Python

这将输出TimeDelta对象,表示2天。

2 days
Python

可以使用Pandas提供的to_period()函数将字符串转换为TimePeriod对象。

import pandas as pd

period_string = "2022-03"
period_period = pd.Period(period_string)
print(period_period)
Python

这将输出TimePeriod对象,表示2022年3月。

2022-03
Python

时间索引

Pandas中的时间索引允许按时间序列访问和操作数据。可以使用Pandas提供的date_range()函数创建时间索引。

import pandas as pd

date_index = pd.date_range("2022-03-01", periods=5, freq="D")
print(date_index)
Python

这将输出一个DatetimeIndex对象,包含从2022年3月1日开始的5天。

DatetimeIndex(['2022-03-01', '2022-03-02', '2022-03-03', '2022-03-04', '2022-03-05'], dtype='datetime64[ns]', freq='D')
Python

可以使用Pandas的resample()函数对时间索引进行重新采样。

import pandas as pd
import numpy as np

date_index = pd.date_range("2022-03-01", periods=5, freq="D")
data = np.random.randn(5)

df = pd.DataFrame(data, index=date_index, columns=["Values"])
monthly_mean = df.resample("M").mean()

print(monthly_mean)
Python

这将输出重新采样为月的平均值的DataFrame对象。

              Values
2022-03-31  0.976110
2022-04-30 -0.389812
Python

总结

在本文中,我们介绍了如何在Pandas中进行日期时间和Pandas Timestamp对象之间的转换。我们讨论了如何使用Pandas的to_datetime()函数将字符串转换为Timestamp对象,并使用strftime()函数将Timestamp对象格式化为不同的字符串格式。我们还研究了如何使用Python的datetime库将Pandas Timestamp对象转换为datetime对象,并使用strftime()函数将datetime对象格式化为不同的字符串格式。我们还研究了如何使用Pandas的to_timedelta()函数将字符串转换为TimeDelta对象,并使用Pandas的to_period()函数将字符串转换为TimePeriod对象。最后,我们了解了如何使用Pandas的date_range()函数创建时间索引,并使用resample()函数重新采样时间索引。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册