gpt4 book ai didi

python - 将相同的 Pandas 数据帧存储到数据库后不相等

转载 作者:行者123 更新时间:2023-11-29 13:07:39 25 4
gpt4 key购买 nike

我有一个 Pandas 数据框:(编辑:实际 df)

date
2019-11-01 07:30:00+00:00 1148.144708
2019-11-01 07:45:00+00:00 1148.015876
2019-11-01 08:00:00+00:00 1147.911068
2019-11-01 08:15:00+00:00 1147.925240
2019-11-01 08:30:00+00:00 1148.007117
2019-11-01 08:45:00+00:00 1148.122853
2019-11-01 09:00:00+00:00 1148.251176
2019-11-01 09:15:00+00:00 1148.362736
2019-11-01 09:30:00+00:00 1148.423236
2019-11-01 09:45:00+00:00 1148.528360
Name: value, dtype: float64

我使用以下方法将它保存到 Postgres 数据库中:

df.to_sql('test_pp', con=engine)

当我从数据库中将其作为 new_df 取回后:(编辑:实际 df)

date
2019-11-01 07:30:00+00:00 1148.144708
2019-11-01 07:45:00+00:00 1148.015876
2019-11-01 08:00:00+00:00 1147.911068
2019-11-01 08:15:00+00:00 1147.925240
2019-11-01 08:30:00+00:00 1148.007117
2019-11-01 08:45:00+00:00 1148.122853
2019-11-01 09:00:00+00:00 1148.251176
2019-11-01 09:15:00+00:00 1148.362736
2019-11-01 09:30:00+00:00 1148.423236
2019-11-01 09:45:00+00:00 1148.528360
Name: value, dtype: float64

这两个数据帧都不相等。这是为什么?

df.equals(new_df)

返回。任何想法?P.S.: 我也检查了顺序和形状,并根据日期排序。

我也检查了索引

DatetimeIndex(['2019-11-01 09:45:00+00:00'], dtype='datetime64[ns, UTC]', name='date', freq=None),
DatetimeIndex(['2019-11-01 09:45:00+00:00'], dtype='datetime64[ns, UTC]', name='date', freq=None))

和数据类型

df['value'].dtypes, new_df['value'].dtypes

返回

(dtype('float64'), dtype('float64'))

奇怪的是,

df['value'].iloc[-1:].index == new_df['value'].iloc[-1:].index

返回

df['value'].iloc[-1].values == new_df['value'].iloc[-1].values

返回

最佳答案

这是因为 double precision在 Postgres 中。它四舍五入了最后 3 位数字。

关于python - 将相同的 Pandas 数据帧存储到数据库后不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59092079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com