重新索引数据帧用NaN替换我的所有数据,为什么?

时间:2021-09-25 19:32:47

So I was investigating how some commands from Pandas work, and I ran into this issue; when I use the reindex command, my data is replaced by NaN values. Below is my code:

所以我正在调查Pandas的一些命令如何工作,我遇到了这个问题;当我使用reindex命令时,我的数据被NaN值替换。以下是我的代码:

>>>import pandas as pd

>>>import numpy as np

>>>frame1=pd.DataFrame(np.arange(365))

then, I give it an index of dates:

然后,我给它一个日期索引:

>>>frame1.index=pd.date_range(pd.datetime(2017, 4, 6), pd.datetime(2018, 4, 5))

then I reindex:

然后我重新索引:

>>>broken_frame=frame1.reindex(np.arange(365))

aaaand all my values are erased. This example isn't particularly useful, but it happens any and every time I use the reindex command, seemingly regardless of context. Similarly, when I try to join two dataframes:

aaa和我的所有价值观都被删除了。这个例子不是特别有用,但是每当我使用reindex命令时它都会发生,看起来无论上下文如何。同样,当我尝试连接两个数据帧时:

>>>big_frame=frame1.join(pd.DataFrame(np.arange(365)), lsuffix='_frame1')

all of the values in the frame being attached (np.arange(365)) are replaced with NaNs before the frames are joined. If I had to guess, I would say this is because the second frame is reindexed as part of the joining process, and reindexing erases my values.

在连接帧之前,所附加的帧中的所有值(np.arange(365))都被NaN替换。如果我不得不猜测,我会说这是因为第二帧被重新索引作为加入过程的一部分,重新​​索引会删除我的值。

What's going on here?

这里发生了什么?

1 个解决方案

#1


1  

From the Docs

来自Docs

Conform DataFrame to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False

使用可选填充逻辑将DataFrame符合到新索引,将NA / NaN放置在先前索引中没有值的位置。除非新索引等效于当前索引并且copy = False,否则将生成新对象

Emphasis my own.

强调我自己。

You want either set_index

你想要set_index

frame1.set_index(np.arange(365))

Or do what you did in the first place

或者做你刚才做的事情

frame1.index = np.arange(365)

#1


1  

From the Docs

来自Docs

Conform DataFrame to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False

使用可选填充逻辑将DataFrame符合到新索引,将NA / NaN放置在先前索引中没有值的位置。除非新索引等效于当前索引并且copy = False,否则将生成新对象

Emphasis my own.

强调我自己。

You want either set_index

你想要set_index

frame1.set_index(np.arange(365))

Or do what you did in the first place

或者做你刚才做的事情

frame1.index = np.arange(365)