1

This is my DataFrame:

               dt_object      Lng
1    2020-01-01 00:00:00  1.57423
2    2020-01-01 01:00:00  1.57444
3    2020-01-01 02:00:00  1.57465
4    2020-01-01 03:00:00  1.57486
5    2020-01-01 04:00:00  1.57506

dt_object prepared with python classic datetime function. But

print (df.columns) 

gives me:

Index(['dt_object', 'Lng'], dtype='object'),

so they both are object types.

I try:

df_candles['dt_object'] = df_candles['dt_object'].astype('datetime64')

but it doesn't help.

It is still object. How can i convert this column to pandas 'datetime64'?

Igor K.
  • 813
  • 6
  • 17

2 Answers2

1

To do that you'd convert it using the to_datetime function

df_candles['dt_object'] = pd.to_datetime(df_candles['dt_object'])

note that if you want, you can also specify the format of your string object as

df_candles['dt_object'] = pd.to_datetime(df_candles['dt_object'], format='%Y-%m-%d %H:%M:%S')

also check https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

Jimmar
  • 4,194
  • 2
  • 28
  • 43
1

Try:

df_candles['dt_object'] = pd.to_datetime(df_candles['dt_object'])

When You are try to check column types use:

df.dtypes

instead of:

df.columns
ipj
  • 3,488
  • 1
  • 14
  • 18