anushya anu
anushya anu

Reputation: 21

change month-year format to year-month-date format in python

Am having a CSV file as input, In which one of the column has date format as Year-Month. I need to change the column format as Year-Month-Date Format, which implies month end date. Am using Python 3. Further , Also am using the modified column in aggregation function and group by function.

Eg: 2020-01

2020-02

2020-03

2020-04

Expected Result :

2020-01-31

2020-02-29 ( Considering leap Year )

2020-03-31

2020-04-30

so on...

Upvotes: 0

Views: 353

Answers (2)

anushya anu
anushya anu

Reputation: 21

import pandas as pd
import datetime as dt
import calendar as cal

# making data frame from csv file 
df = pd.read_csv("Table.csv")

# (month_year)->Column Name
df['month_last_dates'] = [datetime.datetime(date.year, date.month,
      calendar.monthrange(date.year, date.month)[1]) for date in month_year]

# defining aggregation process for each column
aggregations={ 'Metric':sum }

# Group by and aggregate                              
print( df.groupby(['col1', 'month_last_dates','col2']).agg(aggregations) )

Upvotes: 0

Ian Wilson
Ian Wilson

Reputation: 9059

from calendar import monthrange
from datetime import date
def month_end(year, month):
    return date(year=year, month=month, day=monthrange(year, month)[1]).isoformat()
>>> month_end(2020, 2)
'2020-02-29'

Upvotes: 1

Related Questions