2

There are three files with names: file_2018-01-01_01_temp.tif, file_2018-01-01_02_temp.tif and file_2018-01-01_03_temp.tif. I want to list them names as ['2018010101', '2018010102', '2018010103'] in python. The below code create an incorrect list.

import pandas as pd
from glob import glob
from os import path

pattern = '*.tif'
filenames = [path.basename(x) for x in glob(pattern)]
pd.DatetimeIndex([pd.Timestamp(f[5:9]) for f in filenames])

Result: DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01']

1 Answer 1

3

I think simpliest is indexing with replace in list comprehension:

a = [f[5:18].replace('_','').replace('-','') for f in filenames]
print (a)
['2018010101', '2018010102', '2018010103']

Similar with Series.str.replace:

a = pd.Index([f[5:18] for f in filenames]).str.replace('\-|_', '')
print (a)
Index(['2018010101', '2018010102', '2018010103'], dtype='object')

Or convert values to DatetimeIndex and then use DatetimeIndex.strftime:

a = pd.to_datetime([f[5:18] for f in filenames], format='%Y-%m-%d_%H').strftime('%Y%m%d%H')
print (a)
Index(['2018010101', '2018010102', '2018010103'], dtype='object')

EDIT:

dtype is in object, but it must be in dtype='datetime64[ns]

If need datetimes, then formating has to be default, not possible change it:

d = pd.to_datetime([f[5:18] for f in filenames], format='%Y-%m-%d_%H')
print (d)
DatetimeIndex(['2018-01-01 01:00:00', '2018-01-01 02:00:00',
               '2018-01-01 03:00:00'],
              dtype='datetime64[ns]', freq=None)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @jezrael, dtype is in object, but it must be in dtype='datetime64[ns].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.