0

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])] 
4
  • What does df_sku['To_Date'].dtype show? You may need to convert it to np.datetime Commented Apr 8, 2019 at 12:52
  • it's a date column which has 1/1/208, 1/15/2018/, 2/1/208, 2/15/2018.... Commented Apr 8, 2019 at 12:53
  • Well then you need to convert: df_sku['To_Date'] = pd.to_datetime(df_sku, format='%m/%d/%Y') and then try your code Commented Apr 8, 2019 at 12:55
  • no worries, I didn't downvote but you need to post your raw data or sample data, code to recreate your df, the desired output and your current code and errors Commented Apr 8, 2019 at 13:00

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.