'DataFrame with DateTimeIndex from Django model

My goal is to create a pandas dataframe with a datetimeindex from a django model. I am using the django-pandas package for this purpose, specifically, the 'to_timeseries()' method.

First, I used the .values() method on my qs. This still returns a qs, but it contains dictionaries. I then used to_timeseries() to create my dataframe. Everything here worked as expected: the pivot, the values, etc. But my index is just a list of strings. I don't know why.

I have been able to find a great many manipulations in the pandas documentation, including how to turn a column or series into datetime objects. However, my index is not a Series, it is an Index, and none of these methods work. How do I make this happen? Thank you.

df = mclv.to_timeseries(index='day', 
                    pivot_columns='medicine',
                    values='takentoday',
                    storage='wide') 


df = df['day'].astype(Timestamp)
 raise TypeError(f"dtype '{dtype}' not understood")
TypeError: dtype '<class 'pandas._libs.tslibs.timestamps.Timestamp'>' not understood

AttributeError: 'DataFrame' object has no attribute 'DateTimeIndex'

df = pd.DatetimeIndex(df, inplace=True) 
TypeError: __new__() got an unexpected keyword argument 'inplace'

TypeError: Cannot cast DatetimeIndex to dtype

etc...


Solution 1:[1]

Correction & update: django-pandas did work as its authors expected. The problem was my misunderstanding of what it was doing, and how.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Malik A. Rumi