Datetime error

Hi guys, I am getting a datetime error with this code. 


Find entry and exit dates of largest loss

loss_entry = crossover_trade_sheet_sl_tp[crossover_trade_sheet_sl_tp.PnL == crossover_trade_sheet_sl_tp.PnL.min()]['Entry Date'].values[0]

loss_exit = crossover_trade_sheet_sl_tp[crossover_trade_sheet_sl_tp.PnL == crossover_trade_sheet_sl_tp.PnL.min()]['Exit Date'].values[0]


Convert to datetime

loss_entry_date = datetime.datetime.strptime(loss_entry, '%Y-%m-%d')

loss_exit_date = datetime.datetime.strptime(loss_exit, '%Y-%m-%d')


Plot the points on equity curve

plt.figure(figsize=(15, 7))

plt.plot(AAPL_Data['Cumulative_Returns'], color='purple')

plt.plot(loss_entry_date,

         AAPL_Data.loc[loss_entry_date].Cumulative_Returns, 'v', color='r', markersize=8)

plt.plot(loss_exit_date,

         AAPL_Data.loc[loss_exit_date].Cumulative_Returns, 'v', color='r', markersize=8)

plt.title('Equity Curve', fontsize=14)

plt.xlabel('Date')

plt.ylabel('Cumulative Returns')

plt.show()





Here is the error:

 

TypeError                                 Traceback (most recent call last)
Input In [109], in <cell line: 6>()
      3 loss_exit = crossover_trade_sheet_sl_tp[crossover_trade_sheet_sl_tp.PnL == crossover_trade_sheet_sl_tp.PnL.min()]['Exit Date'].values[0]
      5 # Convert to datetime
----> 6 loss_entry_date = datetime.datetime.strptime(loss_entry, '%Y-%m-%d')
      7 loss_exit_date = datetime.datetime.strptime(loss_exit, '%Y-%m-%d')
      9 # Plot the points on equity curve

TypeError: strptime() argument 1 must be str, not numpy.datetime64


What could the issue be? I have imported datetime. 

Hi Jessy,



To convert the date column to datetime format you can consider using the "to_datetime" method of pandas instead.



Since you need to convert the date column, which should be the index of loss_entry dataframe, here's what you can do:

loss_entry.index = pd.to_datetime(loss_entry.index)

Hope this helps!

Thanks
Rushda

Thanks Rushda!