Convert Year/Month/Day to Day of Year in Python

Convert Year/Month/Day to Day of Year in Python

Use datetime.timetuple() to convert your datetime object to a time.struct_time object then get its tm_yday property:

from datetime import datetime
day_of_year = datetime.now().timetuple().tm_yday # returns 1 for January 1st

How to convert year-month-day to just years in python?

Using just base Python (as you didn't specify that you have a pandas dataframe - pandas has specific functions to perform calculations with datetime objects):

from datetime import datetime

#takes as arguments the date as a string and an optional format string
def floatyear(str, fmtstr="%Y-%m-%d"):
t = datetime.strptime(str, fmtstr)
t_first = datetime(t.year, 1, 1, 0, 0, 0)
t_last = datetime(t.year, 12, 31, 0, 0, 0)
return t.year + ((t-t_first).days/(t_last-t_first).days)

print(floatyear("2018-10-24", "%Y-%m-%d"))

Sample output:

2018.8131868131868

Datetime current year and month in Python

Use:

from datetime import datetime
today = datetime.today()
datem = datetime(today.year, today.month, 1)

I assume you want the first of the month.

Convert year/month to year/month/day in python/pandas

I think you need datetimes, so use to_datetime only:

df['date'] = pd.to_datetime(df['date'], format='%Y%m')
print (df)
date
0 2007-05-01
1 2007-06-01
2 2007-07-01
3 2008-08-01

Also if data are read from file add parameter parse_dates:

temp=u"""date
200705
200706
200707
200808"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), parse_dates=['date'])
print (df)
date
0 2005-07-20
1 2006-07-20
2 2007-07-20
3 2008-08-20

But if need strings:

df['date'] = pd.to_datetime(df['date'], format='%Y%m').dt.strftime('%Y/%m/%d')
print (df)
date
0 2007/05/01
1 2007/06/01
2 2007/07/01
3 2008/08/01

Or f-strings (python 3.6+):

df['date'] = [f'{x[:-2]}/{x[-2:]}/01' for x in df['date'].astype(str)]
print (df)
date
0 2007/05/01
1 2007/06/01
2 2007/07/01
3 2008/08/01

Convert month,day,year to month,year with python/pandas?

I think you can use first to_datetime and then to_period:

df.col = pd.to_datetime(df.col).dt.to_period('m')
print (df)
col
0 2009-10
1 2009-12
2 2009-04
3 2007-08
4 2008-07
5 2009-06
6 2009-01
7 2007-12
8 2009-09
9 2006-02
10 2009-03
11 2007-02

print (type(df.loc[0,'col']))
<class 'pandas._period.Period'>

Or strftime:

df.col = pd.to_datetime(df.col).dt.strftime('%m/%Y')
print (df)
col
0 10/2009
1 12/2009
2 04/2009
3 08/2007
4 07/2008
5 06/2009
6 01/2009
7 12/2007
8 09/2009
9 02/2006
10 03/2009
11 02/2007

print (type(df.loc[0,'col']))
<class 'str'>

Or replace by regex:

df.col = df.col.str.replace('/.+/','/')
print (df)
col
0 10/2009
1 12/2009
2 4/2009
3 8/2007
4 7/2008
5 6/2009
6 1/2009
7 12/2007
8 9/2009
9 2/2006
10 3/2009
11 2/2007

print (type(df.loc[0,'col']))
<class 'str'>


Related Topics



Leave a reply



Submit