1

I have a Python program using Pandas, which reads two dataframes, obtained in the following links:

Casos-positivos-diarios-en-San-Nicolas-de-los-Garza-Promedio-movil-de-7-dias: https://datamexico.org/es/profile/geo/san-nicolas-de-los-garza#covid19-evolucion

Denuncias-segun-bien-afectado-en-San-Nicolas-de-los-GarzaClic-en-el-grafico-para-seleccionar: https://datamexico.org/es/profile/geo/san-nicolas-de-los-garza#seguridad-publica-denuncias

What I currently want to do is a groupby in the "covid" dataframe with the same dates, having a sum of these. Regardless, no method has worked out, which regularly prints an error indicating that I should be using a syntaxis for "PeriodIndex". Does anyone have a suggestion or solution? Thanks in advance.

import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib notebook #csv for the covid cases covid = pd.read_csv('Casos-positivos-diarios-en-San-Nicolas-de-los-Garza-Promedio-movil-de-7-dias.csv') #csv for complaints comp = pd.read_csv('Denuncias-segun-bien-afectado-en-San-Nicolas-de-los-GarzaClic-en-el-grafico-para-seleccionar.csv') #cleaning data in both dataframes #keeping only the relevant columns covid = covid[['Month','Daily Cases']] comp = comp[['Month','Affected Legal Good', 'Value']] #changing the labels from spanish to english comp['Affected Legal Good'].replace({'Patrimonio': 'Heritage', 'Familia':'Family', 'Libertad y Seguridad Sexual':'Sexual Freedom and Safety', 'Sociedad':'Society', 'Vida e Integridad Corporal':'Life and Bodily Integrity', 'Libertad Personal':'Personal Freedom', 'Otros Bienes Jurídicos Afectados (Del Fuero Común)':'Other Affected Legal Assets (Common Jurisdiction)'}, inplace=True, regex=True) #changing the month types to dates covid['Month'] = pd.to_datetime(covid['Month']) covid['Month'] = covid['Month'].dt.to_period('M') covid 
3
  • Will you please be able to post the error message ? and the group by code Commented Mar 28, 2022 at 23:20
  • Of course, sorry for that, I lost the code but managed to find it again. covid.groupby(pd.Grouper(key='Month', axis=0, freq='M')).sum() and the error is axis must be a DatetimeIndex, but got an instance of 'PeriodIndex' Commented Mar 28, 2022 at 23:46
  • Hello, did you manage to find a solution? Commented Mar 29, 2022 at 14:20

1 Answer 1

1

You can simply usen group by statement.Timegrouper by default converts it to datetime

import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib notebook #csv for the covid cases covid = pd.read_csv('Casos-positivos-diarios-en-San-Nicolas-de-los-Garza-Promedio-movil-de-7-dias.csv') covid = covid.groupby(['Month'])['Daily Cases'].sum() covid = covid.reset_index() # #changing the month types to dates covid['Month'] = pd.to_datetime(covid['Month']) covid['Month'] = covid['Month'].dt.to_period('M') covid 
Sign up to request clarification or add additional context in comments.

2 Comments

This did the trick, thanks a lot!
No worries glad we fixed it, please upvote!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.