Home > Software design >  python datetime to string to plug into linear regression equation
python datetime to string to plug into linear regression equation

Time:10-27

I believe I need to convert datetime to a string so I can plug this variable into my linear equation

now = datetime.datetime.now()

x = datetime.datetime.strptime(now.astype(str).str[:10], '%Y-%m-%d').toordinal() 

I get this error:

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

or

ValueError: unconverted data remains: 18:17:27.424685 when I try

x = datetime.datetime.strptime(str(now), '%Y-%m-%d').toordinal()

or

AttributeError: 'datetime.datetime' object has no attribute 'astype' when I try

x = datetime.datetime.strptime(now.astype(str).str[:10], '%Y-%m-%d').toordinal()

CodePudding user response:

strptime is a function that converts a string into a datetime object, you have a datetime object already, and you are trying to convert it to a string and back to a datetime object, this is not needed.

My guess is that you are trying to remove the time component of the datetime object in order to get the ordinal of the date, you can more easily do it this way:

now.date().toordinal()

But since toordinal() disregards the time component anyway doing the following will yield the same result:

now.toordinal()
  • Related