MysterioProgrammer91
MysterioProgrammer91

Reputation: 569

Extracting certain elements from a list of a string and turning into datetime with Pandas

I have a a list, which for example is called X. X has number of file names within a particular directory. For example:

X = ['director_send_20140212', 'send_help20150315', 'hello_jeep_20160322'....etc]

Now, I want to extract the dates out of this these lists and convert it into a datetime index of dates so I can use it to index dataframe by dates. So from above example I only want the three dates...but in reality it's a larger collection of file names

To make life easier the dates all begin with 20 (i.e. year 2000 and beyond), and there are no situations where 20 appears anywhere else in the file name. Additionally, the format is yyyymmdd/.

So I want to create a range of dates using the datetimes index provided by pandas!

Upvotes: 1

Views: 101

Answers (1)

ospahiu
ospahiu

Reputation: 3525

I'm making a few assumptions about the naming conventions of your file names. Mainly that the dates are the last portion of data after the underscore, and that the last portion doesn't contain numeric data that isn't the date.

That being said, here is an example list comprehension:

>>> from datetime import datetime
>>> [datetime.strptime(''.join(c for c in file_name.split('_')[-1] if c.isdigit()), "%Y%m%d") for file_name in X]
[datetime.datetime(2014, 2, 12, 0, 0), datetime.datetime(2015, 3, 15, 0, 0), datetime.datetime(2016, 3, 22, 0, 0)]

Upvotes: 1

Related Questions