I’m trying to convert the dates inside a JSON file to their respective quarter and year. My JSON file is formatted below:
{
"lastDate": {
"0": "11/22/2022",
"1": "10/28/2022",
"2": "10/17/2022",
"7": "07/03/2022",
"8": "07/03/2022",
"9": "06/03/2022",
"18": "05/17/2022",
"19": "05/08/2022",
"22": "02/03/2022",
"24": "02/04/2022"
}
}
The current code I’m using is an attempt of using the pandas.Series.dt.quarter
as seen below:
import json
import pandas as pd
data = json.load(open("date_to_quarters.json"))
df = data['lastDate']
pd.to_datetime(df['lastDate'])
df['Quarter'] = df['Date'].dt.quarter
open("date_to_quarters.json", "w").write(
json.dumps(data, indent=4))
The issue I face is that my code isn’t comprehending the object name "lastDate". My ideal output should have the dates ultimately replaced into their quarter, check below:
{
"lastDate": {
"0": "Q42022",
"1": "Q42022",
"2": "Q42022",
"7": "Q32022",
"8": "Q32022",
"9": "Q22022",
"18": "Q22022",
"19": "Q22022",
"22": "Q12022",
"24": "Q12022"
}
}
2
Answers
You can use this bit of code instead:
json
object is different thanpd.DataFrame
. You have to convertjson
topd.DataFrame
first usingfrom_dict()
function.Try:
Prints:
To save
out
as Json: