Wild NaNs appear when adding pandas Series as a column to DataFrame
dataframe, nan, pandas, python
Solution
Thanks to @EdChum comment I found out the problem was caused by indices not matching. This happened because previously I had dropped duplicates from `some_pd_series`, which resulted in "holes" in its index.
Possible ways of solving this issue include:
- `some_pd_series.index = df.index`
- `some_pd_series.reset_index(drop=True, inplace=True)`
Problem
I struggle with a strange bug I cannot understand. Maybe it's something very basic I overlook. The code is following: ``` df = pd.DataFrame( some_numpy_array, columns=[i for i in range(N)]) df.shape (57058, 20) some_pd_series.shape (57058,) df["Text"] = some_pd_series sum(some_pd_series.isnull()) 0 sum(df["Text"].isnull()) 21137 ``` `df["Text"]` should be exactly the same as `some_pd_series`, right? So where do all these `NaN`s suddenly come from?