Python Pandas – 将DateTimeIndex转换为不包含时区的Series
要将DateTimeIndex转换为不包含时区的Series,请使用 datetimeindex.tz_convert(None).to_series() 。其中 tz.convert(None) 用于排除时区。
首先,导入所需的库 –
import pandas as pd
创建周期为5和频率为秒(S)的DatetimeIndex –
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
显示DateTimeIndex –
print("DateTimeIndex...\n", datetimeindex)
将DateTimeIndex转换为Series。使用“tz_convert()”并将其设置为“None”以排除时区 –
print("\nDateTimeIndex转换为不包含时区的系列...\n",
datetimeindex.tz_convert(None).to_series())
示例
以下是代码 –
import pandas as pd
#周期为5,频率为秒(S)
#时区为“Australia/Adelaide”
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
# 显示DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# 显示DateTimeIndex频率
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# 将DateTimeIndex转换为Series
# 使用“tz_convert()”并将其设置为“None”以排除时区
print("\nDateTimeIndex转换为不包含时区的系列...\n",
datetimeindex.tz_convert(None).to_series())
输出
这将产生以下代码 –
DateTimeIndex...
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
'2021-10-18 07:21:12.261811624+10:30',
'2021-10-18 07:21:52.261811624+10:30',
'2021-10-18 07:22:32.261811624+10:30',
'2021-10-18 07:23:12.261811624+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='40S')
DateTimeIndex frequency...
<40 * Seconds>
DateTimeIndex转换为不包含时区的系列...
2021-10-17 20:50:32.261811624 2021-10-17 20:50:32.261811624
2021-10-17 20:51:12.261811624 2021-10-17 20:51:12.261811624
2021-10-17 20:51:52.261811624 2021-10-17 20:51:52.261811624
2021-10-17 20:52:32.261811624 2021-10-17 20:52:32.261811624
2021-10-17 20:53:12.261811624 2021-10-17 20:53:12.261811624
Freq: 40S, dtype: datetime64[ns]