0

I am unable to change the date into the float format. When I use them I get the following error.

return array(a, dtype, copy=False, order=order) ValueError: could not convert string to float: '24/03/2019'

molbdnilo
  • 64,751
  • 3
  • 43
  • 82

1 Answers1

0

You can't parse a string like 24/03/2019 info a float/int since it has non numeric chars.

My advice would be to parse that string date into a datetime:

from datetime import datetime

# without time
datetime_obj = datetime.strptime('24/03/2019', '%d/%m/%Y')
print(datetime_obj.date())

# with time
date_time_str = '2018-06-29 08:15:27.243860'  
date_time_obj = datetime.datetime.strptime('2019-03-24 08:15:27.243860', '%Y-%m-%d %H:%M:%S.%f')
print(datetime_obj.date())

If you really need to parse it into a numeric value and the dates are all like the one you provided, 24/03/2019, I would advise to use a regex and parse it into an int instead of float.

Miguel Machado
  • 341
  • 1
  • 3
  • 6
  • What will be the type of the data after the process you suggested? –  Mar 10 '19 at 17:52
  • It would be a `datetime` object. If you want to know more about what you could do with this type of object check https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior – Miguel Machado Mar 10 '19 at 18:00
  • After converting them, how to split them into different columns, can it be done with the help of split() ? –  Mar 10 '19 at 18:01
  • If you have it in a `datetime` object you don't need to split anything. For example if you want the year: `date_obj.year` – Miguel Machado Mar 10 '19 at 18:22