不幸的是,我无法分享该问题的工作示例,因为我不知道是什么原因造成的。但是,我已经将虚拟代码放在一起,显示了我的 DataFrame 的结构以及我正在尝试做的下采样:
示例代码:
department=[]
team=[]
role=[]
#Department 1 components
department1_A_ROLE1= pd.Series(abs(np.random.randn(5)), index=pd.date_range('01-26-2018',periods=5,freq='B'))
department.append('Department 1')
team.append('A')
role.append('ROLE1')
department1_A_ROLE2= pd.Series(abs(np.random.randn(4)), index=pd.date_range('01-26-2018',periods=4,freq='B'))
department.append('Department 1')
team.append('A')
role.append('ROLE2')
#Department 2 components
department2_B_ROLE1= pd.Series(abs(np.random.randn(7)), index=pd.date_range('01-28-2018',periods=7,freq='B'))
department.append('Department 2')
team.append('B')
role.append('ROLE1')
department2_C_ROLE1= pd.Series(abs(np.random.randn(2)), index=pd.date_range('02-02-2018',periods=2,freq='B'))
department.append('Department 2')
team.append('C')
role.append('ROLE1')
#Department 3 component
department3_B_ROLE2 = pd.Series(abs(np.random.randn(4)), index=pd.date_range('01-31-2018',periods=4,freq='B'))
department.append('Department 3')
team.append('B')
role.append('ROLE2')
#----Generate multi index columns
arrays=[department, team, role]
tuples = list(zip(*arrays))
df=pd.concat([department1_A_ROLE1, department1_A_ROLE2, department2_B_ROLE1, department2_C_ROLE1, department3_B_ROLE2], axis=1)
dateseries=df.index
index = pd.MultiIndex.from_tuples(tuples, names=['Department', 'Team', 'Resource'])
df.columns=index
我的实际 DataFrame 有.shape
(18051, 17)。
重采样:
由此,我尝试.resample
使用以下代码按月计算:
dfByMonth = df.resample('M').sum()
虚拟数据按预期工作:
我的实际 DataFrame 返回.shape
(593, 3 )。
笔记:
- 返回的三列似乎总是相同的三列(来自相同的
department
) - 按字母顺序排序时,返回列不是第一列或最后一列
- 删除多索引 (
df.columns = ' '.join(col).strip() for col in df.columns.values]
) 无效
在 JoeCondron 的评论之后更新:
运行[df.iloc[:,i].apply(type).value_counts() for i in range(df.shape[1])]
给出以下内容-“第 4 部门”是我在...中返回的三列.resample()
...我看到它们是唯一float
没有跟随的列<class 'decimal.Decimal'>
-这看起来像是确凿的证据,但我不明白它们之间的差异......我会认为它们都是数字它们都可以重新采样?(注意:这是通过 Django 响应)
<class 'float'> 1571
<class 'decimal.Decimal'> 30 Name: (Department 1, A, ROLE1), dtype: int64,
<class 'float'> 1571 <class 'decimal.Decimal'> 30 Name: (Department 1, A ROLE2), dtype: int64,
<class 'float'> 1571 <class 'decimal.Decimal'> 30 Name: (Department 1, A ROLE3), dtype: int64,
<class 'float'> 1307 <class 'decimal.Decimal'> 294 Name: (Department 2, A ROLE1), dtype: int64,
<class 'float'> 1307 <class 'decimal.Decimal'> 294 Name: (Department 2, A ROLE2), dtype: int64,
<class 'float'> 1307 <class 'decimal.Decimal'> 294 Name: (Department 2, A ROLE3), dtype: int64,
<class 'decimal.Decimal'> 1281 <class 'float'> 320 Name: (Department 3, A ROLE1), dtype: int64,
<class 'decimal.Decimal'> 1281 <class 'float'> 320 Name: (Department 3, A ROLE2), dtype: int64,
<class 'decimal.Decimal'> 1281 <class 'float'> 320 Name: (Department 3, A ROLE3), dtype: int64,
<class 'float'> 1601 Name: (Department 4, A ROLE1), dtype: int64,
<class 'float'> 1601 Name: (Department 4, A ROLE2), dtype: int64,
<class 'float'> 1601 Name: (Department 4, A ROLE3), dtype: int64,
<class 'decimal.Decimal'> 1601 Name: (Department 5, A ROLE1), dtype: int64,
<class 'float'> 1361 <class 'decimal.Decimal'> 240 Name: (Department 5, A ROLE2), dtype: int64,
<class 'decimal.Decimal'> 1601 Name: (Department 5, A ROLE3), dtype: int64,
<class 'decimal.Decimal'> 1601 Name: (Department 6, A ROLE1), dtype: int64,
<class 'decimal.Decimal'> 1601 Name: (Department 6, A ROLE2), dtype: int64]
<class 'decimal.Decimal'> 1601 Name: (Department 6, A ROLE3), dtype: int64,
<class 'decimal.Decimal'> 1601 Name: (Department 7, A ROLE1), dtype: int64]