Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas to_json - return timestamp with units of days rather than seconds

I have a pandas series containing datetime objects which have been created from day-month-year strings

series = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000'])
series = pd.to_datetime(series)

print series
0   2000-03-11
1   2000-03-12
2   2000-03-13
dtype: datetime64[ns]

Later, after using these datetime objects, I want to convert this series into json in a format day-month-year. However, to_json returns the datetime with HH:MM:SS etc

json = series.to_json(orient='index', date_format='iso', date_unit = 's')
print json
{"0":"2000-03-11T00:00:00Z","1":"2000-03-12T00:00:00Z","2":"2000-03-13T00:00:00Z"}

Is there any inbuilt and elegant way to just return the dates as so

{"0":"2000-03-11","1":"2000-03-12","2":"2000-03-13"}

without the HH:MM:SS etc. The closest I have got (without converting to strings and writing a function to parse) is the date_unit argument of to_json although the largest time unit is seemingly seconds.

Can anyone help? Thanks

like image 450
RoachLord Avatar asked Jan 05 '23 13:01

RoachLord


1 Answers

Something like this?

In [64]: series.dt.date.astype(str).to_json()
Out[64]: '{"0":"2000-03-11","1":"2000-03-12","2":"2000-03-13"}'
like image 66
Ami Tavory Avatar answered Jan 13 '23 10:01

Ami Tavory