JavaEar 专注于收集分享传播有价值的技术资料

ValueError when using the DataFrame.where method in pandas

I am writing the following code, and I want to get only the first 3 minutes of the values with pd.where method, but I get the following error: ValueError: Array conditional must be same shape as self

import pandas as pd
import numpy as np

index = pd.date_range(start = '2017-06-01 00:00', end='2017-06-01 01:00', freq='1min')
values = np.arange(0, len(index))
df = pd.DataFrame(values, index = index)

df.where(df.index <= df.index[0] + pd.DateOffset(minutes=3), np.nan)

There is an another question with this error but the contexts are different.

The code for integer index seems to work well, but for time series I have problem.

1个回答

    最佳答案
  1. You can use df.where after converting df.index to series

    In [557]: df.where(df.index.to_series() <= df.index[0] + pd.DateOffset(minutes=3))
    Out[557]:
                           0
    2017-06-01 00:00:00  0.0
    2017-06-01 00:01:00  1.0
    2017-06-01 00:02:00  2.0
    2017-06-01 00:03:00  3.0
    2017-06-01 00:04:00  NaN
    2017-06-01 00:05:00  NaN
    2017-06-01 00:06:00  NaN
    ...                  ...
    2017-06-01 00:57:00  NaN
    2017-06-01 00:58:00  NaN
    2017-06-01 00:59:00  NaN
    2017-06-01 01:00:00  NaN
    
    [61 rows x 1 columns]