extract date from filename and add as column

Viewed 1368

I have multiple files that i wanted to make into df and merge/append into a single df. The files have common filename pattern, i.e "average map for SWAT [Jan 01,2026]^URTW_L3". I have manage with read through all files and append into one file with added column with the filename. However, got stucked as to how to have the date [Jan 01, 2026] in column instead of the full file names. Essentially, want to extract the date from the filename into a datetime formatted column in the appended final df. Regex method is preferred, as i want to learn how.

import glob
import os
import re
import pandas as pd
myfiles = glob.glob("C:\\Users\\x\\AnacondaProjects\\VORONOI\\test\\*")

col_names = ['X','Y','SW','i']
df = pd.DataFrame()
for file_ in myfiles:
    file_df = pd.read_csv(file_,sep=' ',names=col_names, header=None)
    file__=os.path.split(file_)
    file_df['file_name'] = file__[1]
    df = df.append(file_df)
df.to_csv('merged.csv',index=False,header=True)

Exampe of df so far:-

X   Y   SW  i     file_name
4   3   1   1     average map for SWAT [Jan 01,2026]^URTW_L3
2   1   1   2     average map for SWAT [Jan 01,2027]^URTW_L3

2 Answers

Use extract to get the string enclosed between square brackets, then convert it to Timestamp:

date = df['file_name'].str.extract(r'\[(.+)\]', expand=False)
df['date'] = pd.to_datetime(date, format='%b %d,%Y')

print(df)

   X  Y  SW  i                                       file_name       date
0  4  3   1  1      average map for SWAT [Jan 01,2026]^URTW_L3 2026-01-01
1  2  1   1  2      average map for SWAT [Jan 01,2027]^URTW_L3 2027-01-01

Use the pathlib and dateparser libraries:

from pathlib import Path
from dateparser import parse as dateparse

for file in Path("C:\\Users\\x\\AnacondaProjects\\VORONOI\\test").glob("*"):
    file_name = file.stem
    date = dateparse(file_name)
Related