[英]Python Pandas datetime and multiindex issue
我有一个Python脚本。 运行各种命令以从CSV文件导入,转置和处理数据后,我得到的数据框如下所示:
PV PV
Date 30/11/2016 01/12/2016
00:30 4 4
01:00 5 1
01:30 6 7
etc
我现在想要的是删除2016年11月30日的列,仅保留01/12/2016的数据。 这是我的代码:
# create MultiIndex.from_arrays from first row of DataFrame first, then remove first row
# by df.iloc
df.columns = pd.MultiIndex.from_arrays([df.columns, pd.to_datetime(df.iloc[0])])
df = df.iloc[1:]
# get today's date minus 60 mins. the minus 60 mins will account for the fact that the
# very last half hourly data slot is produced at the beginning of the next day
date = dt.datetime.today() - dt.timedelta(minutes=60)
# convert to correct format:
date = date.strftime("%d-%m-%Y")
# Use indexslice to remove unwanted date columns i.e. none that are not for today's
# date
idx = pd.IndexSlice
df = df.loc[:,idx[:,[date]]]
# drop the second level of the multiindex, which is the level containing the date, which
# is no longer required
df.columns = df.columns.droplevel(1)
在整个11月,直到今天,即12月1日,它开始抛出错误时,都运行良好。 我追踪到的是代码的第一部分,即:
# create MultiIndex.from_arrays from first row of DataFrame first, then remove first row
# by df.iloc
df.columns = pd.MultiIndex.from_arrays([df.columns, pd.to_datetime(df.iloc[0])])
输出为:
PV
Date 2016-11-30 2016-01-12
Date 30/11/2016 01/12/2016
00:30 4 4
01:00 5 1
01:30 6 7
etc
问题出在上面显示的第一组日期中,第一组是2016-11-30,因此是YMD,第二组是2016-01-12,因此是YDM。 为什么日期格式不同? 我如何将它们都保留为YMD?
这有效:
df.columns = pd.MultiIndex.from_arrays([df.columns, pd.to_datetime(df.iloc[0], format='%d/%m/%Y')])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.