'>=' not supported between instances of 'str' and 'datetime.datetime' - used strptime as well

时间:2019-04-08 12:51:03

标签: python python-3.x

I need to select the data frame using my train period shown below but it ran into error always.

train_period = [
['1/1/2018', '10/30/2018']]

train_period = [[datetime.strptime(y,'%m/%d/%Y') for y in x] for x in train_period]

for tp in train_period:
        print()
        #print('Begin:%d End:%d' % (tp[0], tp[1]))
        print()
        df_train_period = df_sku[
                (df_sku['To_Date'] >= tp[begin]) & (df_sku['To_Date'] <= tp[end])]

1 个答案:

答案 0 :(得分:1)

Your 'To_Date' column needs to be of dtype np.datetime in order to do datetime string filtering, so firstly convert first:

df_sku['To_Date'] = pd.to_datetime(df_sku, format='%m/%d/%Y')

then your code will work. You can always check the dtype by calling df_sku['To_Date'].dtype