1

我有一个包含 10 列的“some.xlsb”文件,其中 2 列是DateTime列。

当我使用 pandas 加载时,日期时间列以不同的形式解析。

说明:

其中DateTime值对应于4/10/2021 11:50:24 AM- 读作44296.5

下面是我试过的代码。

goods_df = pd.read_excel('some.xlsb',
                   engine='pyxlsb', sheet_name='goods_df')

goods_df_header = goods_df.iloc[1]
goods_df.columns = goods_df_header #set the header row as the df header
goods_df= goods_df[2:]
goods_df.head(2)
4

1 回答 1

1

当您使用 pandas 读取 xlsb 文件时,您将获得 excel 时间浮点值,因为 xlsb 在存储之前将日期时间对象转换为浮点值。

根据 Microsoft 44296.5 表示自 1900 年 1 月 1 日以来已过去 44296.5 天。

您需要将其转换为纪元,然后使用以下公式确定日期(纪元值 = 自 1970 年 1 月 1 日 00:00:00 以来经过的秒数)。

a = datetime.datetime.strftime((int(<datevalue from excel>)*86400)-2207520000, "%m/%d/%Y")

或者您可以将此 xlsb 保存为 xlsx 并阅读它,您将获得准确的日期时间对象。

于 2021-12-16T06:58:08.027 回答