My current df looks like-
Company State Address Zip City
ABC CA 1 st 86284 LA
All this data is being pulled from files that are monthly dropped in a directory.
I want another column that denotes the month the file was created/dropped in the directory minus 1(File Date minus 1 month). So basically, if the file was created on 5/5/22. I want the below
Company State Address Zip City File Month
ABC CA 1 st 86284 LA 04-2022
My attempt-
import datetime
import os
# Path to the file
path = r"E:\demos\files_demos\sample.txt"
# file creation timestamp in float
c_time = os.path.getctime(path)
# convert creation timestamp into DateTime object
dt_c = datetime.datetime.fromtimestamp(c_time)
df['File Month'] = dt_c
2022-05-05 17:21:57.914774
But this is giving me the above. Is there a better way to do this?
CodePudding user response:
You can subtract a pd.tseries.offsets.MonthEnd
from each date, and use strftime
:
df['File Month'] = df['File Month'].sub(pd.tseries.offsets.MonthEnd()).dt.strftime('%m-%Y')
CodePudding user response:
You can calculate previous month from your datetime object and convert it to your desired string format
df['File Month'] = (dt_c.replace(day=1) - datetime.timedelta(days=1)).strftime("%m-%Y")
Company State Address Zip City File Month
0 ABC CA 1 st 86284 LA 04-2022