1

I'd like to make a new column in dataframe df that will fill all rows with [np.nan]

    df['new'] = [np.nan]

I get

ValueError: Length of values does not match length of index

If I try

    test['new'] = np.nan
    test['new'] = test['new'].astype('object')
    test['new'] = [np.nan]

I get

ValueError: Length of values does not match length of index

I'd like to be sure that all rows are filled with a list containing nan

4
  • 1
    Hold on , you want np.nan in each cell or [np.nan] ? Commented Aug 6, 2018 at 21:44
  • Each cell yes, that's correct. Commented Aug 6, 2018 at 21:45
  • 1
    Why would you want [np.nan] instead of np.nan? That's inefficient and highly inadvisable. Your series will become object type (a sequence of pointers) rather than float. Commented Aug 6, 2018 at 21:45
  • Because it will be loaded into a Postgresql table that has a column of array type: bigint[]. It has to match data type Commented Aug 6, 2018 at 21:47

1 Answer 1

5

You are assign a object

df['New']=[[np.nan]]*len(df)
df
Out[250]: 
        Date  Value    New
0 2017-01-01      1  [nan]
1 2017-02-13      2  [nan]
2 2018-03-01      3  [nan]
3 2018-04-01      4  [nan]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.