0

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...

  • What have you tried so far? – Ian Wilson Jul 12 '20 at 04:05
  • 1
    Does this answer your question? [How to get the last day of the month?](https://stackoverflow.com/questions/42950/how-to-get-the-last-day-of-the-month) – sushanth Jul 12 '20 at 04:20
  • @Sushanth .. No , because am using CSV file.. Am using the modified column in aggregations function and Group by function. There am facing issue. – anushya anu Jul 12 '20 at 04:32
  • Your problem statment is incorrect & does not mention any thing about aggregation or groupby. you might need to redfine the problem statment with appropriate input & expected output. – sushanth Jul 12 '20 at 04:42

2 Answers2

1
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'
Ian Wilson
  • 6,223
  • 1
  • 16
  • 24
  • 1
    While this code may answer the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would help to improve the quality of your answer. Remember that you are also answering the question for readers in the future, not just the person asking now. – herrbischoff Jul 12 '20 at 19:42
0
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) )
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43