I tried to understand how covid cases are changing since pandemic begun, and NYT provides daily covid cases data by county level. I used the data and want to see how new casea are changing historically. To do so, I used pandas builtin function diff() to do get difference, but I got wrong output, somewhere in the output, it got negative value, which I don't understand why. Can anyone point me out what goes wrong by using pandas.diff()? Any idea?
current attempt
Here is the NYT covid data statistics repos, and I attempted following:
import pandas as pd
url = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"
df= pd.read_csv(url, parse_dates=['date'])
df.drop(columns=['Unnamed: 0'], inplace=True)
df['new_cases'] = df.groupby(['county'])['cases'].diff()
df['new_deaths'] = df.groupby(['county'])['deaths'].diff()
df= df.fillna(0)
if we run this code, new cases for some counties got negative values which doesn't make sense. what I want to do is, how many cases is increasing, or remain same or decreasing respect to previous days. I don't understand why pandas.diff() goes wrong. What's the more accurate and secure way to do this in pandas? Any thoughts?
new update
if above attempt goes right, this is my next step that I want to make plot, and I did like this by continuing above attempt:
df['new_cases_rol7'] = df[['new_cases']].rolling(7).mean()
but problem is new_cases calculation goes wrong.
I spotted negative new_cases value doing df.to_csv("file.csv") and opened it on ms excel. I spot this issue because when I did plot, some plots looks so off, I think pandas.diff() goes wrong. any idea what goes wrong?