0

I was wondering is there any idea about how to do resampling on DateTime indices other than DatetimeIndex, TimedeltaIndex or PeriodIndex?

from datetime import timedelta
from khayyam import JalaliDate, JalaliDatetime
import pandas as pd

start_date_jalali = JalaliDate(1399, 5, 1)
end_date_jalali = JalaliDate(1400, 5, 31)
date = [start_date_jalali + timedelta(days=i) for i in range((end_date_jalali - start_date_jalali).days + 1)]
df = pd.DataFrame(index=date, columns=[])
df.index.name = 'Date'

df.resample('M')
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

I think a simple workaround is to convert Jalali dates to Georgian dates and then apply resampling on them where the end of each period in Georgian must be the last day of the month in the Jalali calendar, as implemneted here.

sci9
  • 700
  • 1
  • 7
  • 21
  • try `pd.DataFrame(index=pd.to_datetime(date), columns=[])` then `df.resample('M')` – Anurag Dabas Aug 13 '21 at 07:13
  • @AnuragDabas Thank you for comment but `pd.to_datetime()` cannot be applied to khayyam datetimes: `TypeError: is not convertible to datetime` – sci9 Aug 13 '21 at 07:18

1 Answers1

1

IIUC:

That's what error is saying try converting your index to datetime:

use map()+todate() method for converting 'JalaliDate' to 'datetime64' and then resample():

df.index=pd.to_datetime(df.index.map(lambda x:x.todate()))
#Finally:
df.resample('M')
#Further do calculations

OR

change your list comprehension to:

date=[pd.to_datetime((start_date_jalali + timedelta(days=i)).todate()) for i in range((end_date_jalali - start_date_jalali).days + 1)]

#Finally:
df = pd.DataFrame(index=date, columns=[])
df.index.name = 'Date'
df.resample('M')
#Further do calculations
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
  • df.index = [x.todate() for x in df.index] dose the same thing. – sci9 Aug 13 '21 at 07:39
  • @sci9 yes you can also do `df.index=pd.to_datetime([x.todate() for x in df.index])` or `df.index=[pd.to_datetime(x.todate()) for x in df.index]` it's up to you – Anurag Dabas Aug 13 '21 at 07:44
  • Thank you for your answer, but the resulted df computes the aggregation based on the last month of the Georgian calendar: `Index: [2020-07-31 00:00:00, 2020-08-31 00:00:00, 2020-09-30 00:00:00` – sci9 Aug 13 '21 at 07:45
  • @sci9 since the 'JalaliDate' month '5' is corresponding to 'Georgian' month '7' and like this variation in year and day so I think it correctly computes aggregration – Anurag Dabas Aug 13 '21 at 08:01