Ashok
Ashok

Reputation: 327

Converting List of list strings into date in python for time series modelling -

My requirement is to consider data points for certain range.

as given below

train_period = [
['7-31-2013', '12-31-2014'],
['8-31-2013', '1-31-2015'],
['9-31-2013', '2-28-2015'],
['10-31-2013','3-31-2015'],
['11-31-2013', '4-30-2015']
]

But python is differentiating them, when i give without Quotes. So I took them as string but while converting the strings to dates using below code, I got error as

"Type Error: strptime() argument 1 must be str, not list"

DATE = [datetime.strptime(x,'%m/%d/%Y %H:%M') for x in train_period]

I know this is because of list of list. Please help how to resolve.

Upvotes: 2

Views: 310

Answers (2)

Allan
Allan

Reputation: 12438

Depending on the result you want to have use either one of those two list comprehension operators:

  • The first one will give you a list of list
  • The second one will give you a flat list

Code:

from datetime import datetime

train_period = [
['7-31-2013', '12-31-2014'],
['8-31-2013', '1-31-2015'],
['9-30-2013', '2-28-2015'],
['10-31-2013','3-31-2015'],
['11-30-2013', '4-30-2015']
]


DATE = [[datetime.strptime(y,'%m-%d-%Y') for y in x] for x in train_period]
print(DATE)
print("====")
DATE = [datetime.strptime(y,'%m-%d-%Y') for x in train_period for y in x]
print(DATE)

Output:

[[datetime.datetime(2013, 7, 31, 0, 0), datetime.datetime(2014, 12, 31, 0, 0)], [datetime.datetime(2013, 8, 31, 0, 0), datetime.datetime(2015, 1, 31, 0, 0)], [datetime.datetime(2013, 9, 30, 0, 0), datetime.datetime(2015, 2, 28, 0, 0)], [datetime.datetime(2013, 10, 31, 0, 0), datetime.datetime(2015, 3, 31, 0, 0)], [datetime.datetime(2013, 11, 30, 0, 0), datetime.datetime(2015, 4, 30, 0, 0)]]
====
[datetime.datetime(2013, 7, 31, 0, 0), datetime.datetime(2014, 12, 31, 0, 0), datetime.datetime(2013, 8, 31, 0, 0), datetime.datetime(2015, 1, 31, 0, 0), datetime.datetime(2013, 9, 30, 0, 0), datetime.datetime(2015, 2, 28, 0, 0), datetime.datetime(2013, 10, 31, 0, 0), datetime.datetime(2015, 3, 31, 0, 0), datetime.datetime(2013, 11, 30, 0, 0), datetime.datetime(2015, 4, 30, 0, 0)]

Also you should change your pattern for the timestamp to %m-%d-%Y otherwise you will have some errors as datetime can not interpret the String and build a date object.

ValueError: time data '7-31-2013' does not match format '%m-%d-%Y %H:%M'

Last but not least, your string must be valid dates!!! (there is no 31st of Sept or 31st of Nov)

Upvotes: 1

iz_
iz_

Reputation: 16613

Simply use a nested list comprehension:

from datetime import datetime

train_period = [
['7-31-2013', '12-31-2014'],
['8-31-2013', '1-31-2015'],
['9-31-2013', '2-28-2015'],
['10-31-2013','3-31-2015'],
['11-31-2013', '4-30-2015']
]

DATE = [[datetime.strptime(y,'%m/%d/%Y %H:%M') for y in x] for x in train_period]

Note that your pattern is wrong. Try %m-%d-&Y. However, your data contains a September 31st and a November 31st, so you will need to fix that.

Upvotes: 2

Related Questions