简体   繁体   中英

How can I get the date a file was created into my dataframe in Python?

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?

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')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM