younggotti
younggotti

Reputation: 822

Numpy.where on same column resulting in an empty dataframe

I have a date stored in a datetime64[ns] variable called "IBKR_cutoff_date".

I also have a dataframe called prev1_contract:

         date  Price_PREV_1  OI_PREV_1
0  2021-02-22        523.50      52741
1  2021-02-23        528.75      52383
2  2021-02-24        539.25      52842
3  2021-02-25        541.00      51831
4  2021-02-26        531.00      52839
..        ...           ...        ...
95 2021-07-06        598.75      74271
96 2021-07-07        585.50      57216
97 2021-07-08        592.25      31202
98 2021-07-09        610.00       5071
99 2021-07-12        610.00          0

I want to check if dates in column "date" (whose dtype is datetime64[ns]) are greater than IBKR_cutoff_date. If so, dataframe "Price_PREV_1" column must be set as blank (overwriting previous values), otherwise the column's existing value is kept.

I tried:

prev1_contract['Price_PREV_1'] = np.where(prev1_contract['date']>IBKR_cutoff_date, '', prev1_contract['Price_PREV_1'] ).astype(float)

This results in a strange behaviour:

What am I missing? Thanks

Upvotes: 1

Views: 201

Answers (1)

IoaTzimas
IoaTzimas

Reputation: 10624

Just remove .astype(float) from the end of your code, as '' cannot be converted to float

Upvotes: 1

Related Questions