为什么要做' attrs' PANDAS DataFrames中的系列更改为DataFrame' attrs'副本后

发布于 2025-02-11 12:44:41 字数 1630 浏览 1 评论 0原文

当我有一个具有不同attrs的dataframe时,数据帧及其包含的系列都在复制dataframe之后,原始实例具有所有系列的dataframe的attrs好吧,该系列的attrs丢失了。

要明显,我不是在谈论复制的实例,而是谈论原始实例。

这是正在发生的事情的示例:

df = pd.DataFrame([1, 2])
df.attrs['a'] = 'b'
df[0].attrs['c'] = 'd'
print(f"DataFrame attrs: {df.attrs} /  Series attrs: {df[0].attrs}")
df_copy = df.copy()
print(f"DataFrame after copy attrs: {df.attrs} /  Series attrs: {df[0].attrs}")
print(f"Copied DataFrame attrs: {df_copy.attrs} /  Series attrs: {df_copy[0].attrs}")
<<<
DataFrame attrs: {'a': 'b'} /  Series attrs: {'a': 'b', 'c': 'd'}
DataFrame after copy attrs: {'a': 'b'} /  Series attrs: {'a': 'b'}
Copied DataFrame attrs: {'a': 'b'} /  Series attrs: {'a': 'b'}

另请注意'a':'a':'b'在系列attrs中以某种方式从dataframe复制,我也找到了那个奇怪的是,它是错误还是功能?

我还尝试使用_metadata 解决方案而不是或多或少地以相似的方式行事。它可能使用相同的基础逻辑:

df = pd.DataFrame([1, 2])
df._metadata.append('meta')
df.meta = 'a'
df[0]._metadata.append('meta')
df[0].meta = 'b'
print(f"DataFrame meta: {df.meta} /  Series meta: {df[0].meta}")
df_copy = df.copy()
print(f"DataFrame after copy meta: {df.meta} /  Series meta: {df[0].meta}")
print(f"Copied DataFrame meta: {df_copy.meta} /  Series meta: {df_copy[0].meta}")
<<<
DataFrame meta: a /  Series meta: b
DataFrame after copy meta: a /  Series meta: a
Copied DataFrame meta: a /  Series meta: a

我正在使用Pandas版本1.4.2和Python 3.9

When I have a DataFrame with different attrs values for both the DataFrame and the Series it contains then after copying the DataFrame the original instance has the attrs of the DataFrame for all Series as well and the attrs of the Series is lost.

To be clear I'm not talking about the copied instance but about the original instance.

Here is an example of what is going on:

df = pd.DataFrame([1, 2])
df.attrs['a'] = 'b'
df[0].attrs['c'] = 'd'
print(f"DataFrame attrs: {df.attrs} /  Series attrs: {df[0].attrs}")
df_copy = df.copy()
print(f"DataFrame after copy attrs: {df.attrs} /  Series attrs: {df[0].attrs}")
print(f"Copied DataFrame attrs: {df_copy.attrs} /  Series attrs: {df_copy[0].attrs}")
<<<
DataFrame attrs: {'a': 'b'} /  Series attrs: {'a': 'b', 'c': 'd'}
DataFrame after copy attrs: {'a': 'b'} /  Series attrs: {'a': 'b'}
Copied DataFrame attrs: {'a': 'b'} /  Series attrs: {'a': 'b'}

Also note the entry 'a': 'b' in the Series attrs which is somehow copied from the DataFrame, I also find that strange, is it a bug or a feature?

I've also tried using the _metadata solution instead but this behaves in a more or less similar way. It might use the same underlying logic:

df = pd.DataFrame([1, 2])
df._metadata.append('meta')
df.meta = 'a'
df[0]._metadata.append('meta')
df[0].meta = 'b'
print(f"DataFrame meta: {df.meta} /  Series meta: {df[0].meta}")
df_copy = df.copy()
print(f"DataFrame after copy meta: {df.meta} /  Series meta: {df[0].meta}")
print(f"Copied DataFrame meta: {df_copy.meta} /  Series meta: {df_copy[0].meta}")
<<<
DataFrame meta: a /  Series meta: b
DataFrame after copy meta: a /  Series meta: a
Copied DataFrame meta: a /  Series meta: a

I'm using Pandas version 1.4.2 and python 3.9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文