Python 将月数转换为日期时间对象时出错

Python 将月数转换为日期时间对象时出错,python,python-2.7,pandas,datetime,Python,Python 2.7,Pandas,Datetime,尝试将数据帧中的一列从1-420个月(从1985年到2010年的35年月度数据)转换为datetime对象 示例数据帧: import pandas as pd import numpy as np dates = pd.Series(range(1,421)) df2 = pd.DataFrame(np.random.randn(420,4),index=dates,columns=list('ABCD')) 将索引转换为日期时间对象: df2.index = pd.to_datetime(

尝试将数据帧中的一列从1-420个月(从1985年到2010年的35年月度数据)转换为datetime对象

示例数据帧:

import pandas as pd
import numpy as np
dates = pd.Series(range(1,421))
df2 = pd.DataFrame(np.random.randn(420,4),index=dates,columns=list('ABCD'))
将索引转换为日期时间对象:

df2.index = pd.to_datetime(df2.index,unit='M', origin='1981-01-01')
给出了错误:

ValueError: cannot cast unit M
我不知道为什么它不会使用单位M,因为当我尝试使用“d”而不是“M”时,它会工作,并且每天都在上升——为什么它不会每月上升?我从你那里得到了单位

使用“m”输出如下所示:

                       A           B            C            D
1981-01-01 00:01:00 0.672397    0.753926    0.865845    0.711594
1981-01-01 00:02:00 0.786754    0.658421    -0.111609   -1.459447
1981-01-01 00:03:00 0.200273    -1.485525   -1.939203   0.921833
1981-01-01 00:04:00 -1.589668   0.109760    -1.349790   -1.951316
1981-01-01 00:05:00 0.133847    -0.359300   -1.246740   -0.835645
1981-01-01 00:06:00 -0.843962   1.222129    -0.121450   -1.223132
1981-01-01 00:07:00 -0.818932   0.731127    0.984731    -1.028384
                            A           B            C           D
    1981-01-01 00:00:00 0.672397    0.753926    0.865845    0.711594
    1981-02-01 00:00:00 0.786754    0.658421    -0.111609   -1.459447
    1981-03-01 00:00:00 0.200273    -1.485525   -1.939203   0.921833
它在几分钟内上升,我希望它在几个月内上升,就像这样:

                       A           B            C            D
1981-01-01 00:01:00 0.672397    0.753926    0.865845    0.711594
1981-01-01 00:02:00 0.786754    0.658421    -0.111609   -1.459447
1981-01-01 00:03:00 0.200273    -1.485525   -1.939203   0.921833
1981-01-01 00:04:00 -1.589668   0.109760    -1.349790   -1.951316
1981-01-01 00:05:00 0.133847    -0.359300   -1.246740   -0.835645
1981-01-01 00:06:00 -0.843962   1.222129    -0.121450   -1.223132
1981-01-01 00:07:00 -0.818932   0.731127    0.984731    -1.028384
                            A           B            C           D
    1981-01-01 00:00:00 0.672397    0.753926    0.865845    0.711594
    1981-02-01 00:00:00 0.786754    0.658421    -0.111609   -1.459447
    1981-03-01 00:00:00 0.200273    -1.485525   -1.939203   0.921833
你应使用:

输出:

                A           B           C            D
1981-01-01  -0.761933   0.726808    0.589712    -1.170934
1981-02-01  0.030521    -0.892427   -1.366809   -1.515724
1981-03-01  -0.282887   1.068047    0.244493    -0.247356
有关更多信息,请查看

编辑:正如OP所说,425天重复了200000行。下面的代码将提供重复的索引

daterange = pd.date_range('1981/1/1', periods=420, freq='MS') 
然后通过重复它来扩展它以适合您的数据帧

df2.index = list(daterange) * math.floor(len(df2)/len(list(daterange))) + list(daterange)[0:math.floor(len(df2)%len(list(daterange)))]
你应使用:

输出:

                A           B           C            D
1981-01-01  -0.761933   0.726808    0.589712    -1.170934
1981-02-01  0.030521    -0.892427   -1.366809   -1.515724
1981-03-01  -0.282887   1.068047    0.244493    -0.247356
有关更多信息,请查看

编辑:正如OP所说,425天重复了200000行。下面的代码将提供重复的索引

daterange = pd.date_range('1981/1/1', periods=420, freq='MS') 
然后通过重复它来扩展它以适合您的数据帧

df2.index = list(daterange) * math.floor(len(df2)/len(list(daterange))) + list(daterange)[0:math.floor(len(df2)%len(list(daterange)))]

这只需要几分钟,而不是几个月,当我尝试大写字母M数月时,它给出了相同的错误这适用于此示例数据,但不适用于我的实际数据-对于我的实际数据,我需要将包含1:420数字的列转换为1981年1月1日至2015年12月1日的列。这对它不起作用,因为我得到了错误
ValueError:值的长度与索引的长度不匹配
。它与索引的长度不匹配,因为索引的行数为200000行,但数字1-420在整个过程中重复出现。我需要将每个1转换为1981年1月1日,每个2转换为1981年2月1日,等等。当我将datetime参数转换为天,而不是数月时,这项功能就起作用了。看起来这个功能@Pad You是对的。尽管如此,还是有一个解决办法。我试过你的问题有420多行。这给了我一个重复索引。试试这个。让我知道它是否有效。获取要重复的对象列表
daterange=pd.date\u range('1981/1/1',periods=420,freq='MS')
然后通过重复将其展开以适合您的数据帧
df2.index=list(daterange)*math.floor(len(df2)/len(list(daterange))+list(daterange)[0:math.floor(len(df2)%len(list(daterange))]
谢谢-很抱歉,我现在只看到这个,因为我离开了wifi。当我尝试将您所做的应用于我的数据帧中的列时,我得到了错误
TypeError:不能将序列乘以类型为“float”的非int
-当我尝试转换为int时,它会给出更多错误!这只需要几分钟,而不是几个月,当我尝试大写字母M数月时,它给出了相同的错误这适用于此示例数据,但不适用于我的实际数据-对于我的实际数据,我需要将包含1:420数字的列转换为1981年1月1日至2015年12月1日的列。这对它不起作用,因为我得到了错误
ValueError:值的长度与索引的长度不匹配
。它与索引的长度不匹配,因为索引的行数为200000行,但数字1-420在整个过程中重复出现。我需要将每个1转换为1981年1月1日,每个2转换为1981年2月1日,等等。当我将datetime参数转换为天,而不是数月时,这项功能就起作用了。看起来这个功能@Pad You是对的。尽管如此,还是有一个解决办法。我试过你的问题有420多行。这给了我一个重复索引。试试这个。让我知道它是否有效。获取要重复的对象列表
daterange=pd.date\u range('1981/1/1',periods=420,freq='MS')
然后通过重复将其展开以适合您的数据帧
df2.index=list(daterange)*math.floor(len(df2)/len(list(daterange))+list(daterange)[0:math.floor(len(df2)%len(list(daterange))]
谢谢-很抱歉,我现在只看到这个,因为我离开了wifi。当我尝试将您所做的应用于我的数据帧中的列时,我得到了错误
TypeError:不能将序列乘以类型为“float”的非int
-当我尝试转换为int时,它会给出更多错误!预期输出是什么?@harv我编辑了问题预期输出是什么?@harv我编辑了问题