Python 如何向下转换整数多索引级别? 我有一个大规模多索引系列的字典,其中两个索引级别都是日期时间值。 其中一个抽象的简短示例是: 我需要保存所有数据,所以我选择将每个系列分别导入到公共HDF5文件中,并使用dictionary键作为hdf键。当我将其保存为原样时,我的文件卷大约为4GB,因此我正在尝试使其更薄。此外,我还需要跨索引处理所有系列数据,所以我需要一些全局标识方法。 我的想法是从所有系列中的两个级别共同收集日期(大约有11000个唯一日期),并用唯一编号标识符替换它,以便有机会恢复所有系列的原始索引。但只有当我能够将数值转换为int16类型时,它才有意义。 因此,我尝试了这样一个序列(这里我将其简化为单个系列):
这似乎是一个成功:Python 如何向下转换整数多索引级别? 我有一个大规模多索引系列的字典,其中两个索引级别都是日期时间值。 其中一个抽象的简短示例是: 我需要保存所有数据,所以我选择将每个系列分别导入到公共HDF5文件中,并使用dictionary键作为hdf键。当我将其保存为原样时,我的文件卷大约为4GB,因此我正在尝试使其更薄。此外,我还需要跨索引处理所有系列数据,所以我需要一些全局标识方法。 我的想法是从所有系列中的两个级别共同收集日期(大约有11000个唯一日期),并用唯一编号标识符替换它,以便有机会恢复所有系列的原始索引。但只有当我能够将数值转换为int16类型时,它才有意义。 因此,我尝试了这样一个序列(这里我将其简化为单个系列):,python,pandas,multi-index,downcast,Python,Pandas,Multi Index,Downcast,这似乎是一个成功: print('df_info_downcasted column types:\n', df_info_downcasted.dtypes) 结果表明: df_info_downcasted column types: Event_Date int16 Observation_Date int16 Some_Values float64 但当我将列移回索引级别时,它又变为int64: 我尝试了其他操作,但也失败了: 因此,我
print('df_info_downcasted column types:\n', df_info_downcasted.dtypes)
结果表明:
df_info_downcasted column types:
Event_Date int16
Observation_Date int16
Some_Values float64
print('df_info_downcasted column types:\n', df_info_downcasted.dtypes)
df_info_downcasted column types:
Event_Date int16
Observation_Date int16
Some_Values float64
ser_info_downcasted = df_info_downcasted.set_index(['Event_Date', 'Observation_Date']).squeeze()
print('ser_info_downcasted index level 0 type: ', ser_info_downcasted.index.levels[0].dtype)
print('ser_info_downcasted index level 1 type: ', ser_info_downcasted.index.levels[1].dtype)
ser_info_downcasted index level 0 type: int64
ser_info_downcasted index level 1 type: int64
ser_info_astyped = ser_info_downcasted.copy()
ser_info_astyped.index = ser_info_astyped.index.set_levels(ser_info_astyped.index.levels[0].astype('int16'), level = 0)
ser_info_astyped.index = ser_info_astyped.index.set_levels(ser_info_astyped.index.levels[1].astype('int16'), level = 1)
print('ser_info_astyped index level 0 type: ', ser_info_astyped.index.levels[0].dtype)
print('ser_info_astyped index level 1 type: ', ser_info_astyped.index.levels[1].dtype)
ser_info_astyped index level 0 type: int64
ser_info_astyped index level 1 type: int64