在 Python 数据框中将索引定义为日期时间
Defining an index as date time in Python dataframe
使用 R 多年后,我开始尝试并学习 python,从编辑一些现有代码开始。
我有一个从 netCDF 文件导入的时间变量,如下所示:
import netCDF4 as nc
import numpy as np
import datetime
import pandas as pd
f = nc.Dataset(fname)
time = nc.num2date(f.variables['time'][:],
f.variables['time'].units)
nc_dims = [dim for dim in f.dimensions]
时间变量被添加到数据帧 df
中名为 'dates'
的列中:
df['dates'] = time
然后转换为索引:
df = df.set_index('dates')
为数据框定义了两个新列,都要求索引具有正确的日期时间数据格式(我认为)。
df['season'] = (df.index.month) % 12 + 3) // 3
df['day'] = np.floor(df.index.to_julian_date().values - 0.5)
但是,当命令提示符中的代码为运行时,错误结果为:
AttributeError: 'Index' object has no attribute 'month' ; and
'Index' object has no attribute 'to_julian_date'
这让我认为索引对象未正确定义为日期时间对象(因此月份调用不起作用)并且它不知道它可以将对象转换为儒略日期格式。要么,要么我错过了一个重要的包裹。
我尝试使用以下方法转换索引:
pd.Timestamp(time)
df = df.set_index(pd.DatetimeIndex('dates'))
df.index = pd.to_datetime(df.index)
但是得到错误提示索引对象已经是日期时间格式:
TypeError: Cannot convert input [[cftime.DatetimeGregorian(2011-01-01
00:30:00.13) TypeError: is
not convertible to datetime TypeError: is not convertible to datetime
我觉得我错过了一个重要的拼图,因为我知道这段代码在我的同事环境中 运行。任何人都可以提供的任何帮助都可以帮助我调试,非常感谢!
编辑:
这里是一小部分数据:
iveg_1 iveg_2 patchfrac_1 patchfrac_2
dates
2011-01-01 00:30:00.13 2 6 1.0 0.0
2011-01-01 01:00:00 2 6 1.0 0.0
2011-01-01 01:30:00 2 6 1.0 0.0
2011-01-01 02:00:00.13 2 6 1.0 0.0
2011-01-01 02:30:00 2 6 1.0 0.0
2011-01-01 03:00:00 2 6 1.0 0.0
2011-01-01 03:30:00.13 2 6 1.0 0.0
2011-01-01 04:00:00 2 6 1.0 0.0
2011-01-01 04:30:00 2 6 1.0 0.0
2011-01-01 05:00:00.13 2 6 1.0 0.0
首先尝试转换为字符串:
df['dates'] = time
df['dates'] = pd.to_datetime(df['dates'].astype('str'))
df = df.set_index('dates')
使用 df.info()
验证您正在使用 DateTimeIndex
。
然后 DateTimeIndex
对象将允许您从中提取日期信息,例如它的月份或转换 .to_julian_date()
.
使用 R 多年后,我开始尝试并学习 python,从编辑一些现有代码开始。
我有一个从 netCDF 文件导入的时间变量,如下所示:
import netCDF4 as nc
import numpy as np
import datetime
import pandas as pd
f = nc.Dataset(fname)
time = nc.num2date(f.variables['time'][:],
f.variables['time'].units)
nc_dims = [dim for dim in f.dimensions]
时间变量被添加到数据帧 df
中名为 'dates'
的列中:
df['dates'] = time
然后转换为索引:
df = df.set_index('dates')
为数据框定义了两个新列,都要求索引具有正确的日期时间数据格式(我认为)。
df['season'] = (df.index.month) % 12 + 3) // 3
df['day'] = np.floor(df.index.to_julian_date().values - 0.5)
但是,当命令提示符中的代码为运行时,错误结果为:
AttributeError: 'Index' object has no attribute 'month' ; and
'Index' object has no attribute 'to_julian_date'
这让我认为索引对象未正确定义为日期时间对象(因此月份调用不起作用)并且它不知道它可以将对象转换为儒略日期格式。要么,要么我错过了一个重要的包裹。
我尝试使用以下方法转换索引:
pd.Timestamp(time)
df = df.set_index(pd.DatetimeIndex('dates'))
df.index = pd.to_datetime(df.index)
但是得到错误提示索引对象已经是日期时间格式:
TypeError: Cannot convert input [[cftime.DatetimeGregorian(2011-01-01 00:30:00.13) TypeError: is not convertible to datetime TypeError: is not convertible to datetime
我觉得我错过了一个重要的拼图,因为我知道这段代码在我的同事环境中 运行。任何人都可以提供的任何帮助都可以帮助我调试,非常感谢!
编辑:
这里是一小部分数据:
iveg_1 iveg_2 patchfrac_1 patchfrac_2
dates
2011-01-01 00:30:00.13 2 6 1.0 0.0
2011-01-01 01:00:00 2 6 1.0 0.0
2011-01-01 01:30:00 2 6 1.0 0.0
2011-01-01 02:00:00.13 2 6 1.0 0.0
2011-01-01 02:30:00 2 6 1.0 0.0
2011-01-01 03:00:00 2 6 1.0 0.0
2011-01-01 03:30:00.13 2 6 1.0 0.0
2011-01-01 04:00:00 2 6 1.0 0.0
2011-01-01 04:30:00 2 6 1.0 0.0
2011-01-01 05:00:00.13 2 6 1.0 0.0
首先尝试转换为字符串:
df['dates'] = time
df['dates'] = pd.to_datetime(df['dates'].astype('str'))
df = df.set_index('dates')
使用 df.info()
验证您正在使用 DateTimeIndex
。
然后 DateTimeIndex
对象将允许您从中提取日期信息,例如它的月份或转换 .to_julian_date()
.