sci9
sci9

Reputation: 776

Python Pandas: Resampling on Jalali datetime indices

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.

Upvotes: 0

Views: 472

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

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

Upvotes: 1

Related Questions