addypy / datagovindia

Python Client for India’s - Open Government Data (OGD) (https://data.gov.in/) platform APIs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I am not able to apply a date filter

sangameshks opened this issue · comments

I know the date filter can be applied on OGD API. can you show one example. I have been trying it multiple times for APMC mandi data. Please help me out

Thanks in advance

Regards,
Sangamesh KS

@sangameshks - We're aware of this issue. The present version of our library does not have support date-filtering. This is because there are several challenges to adding date filtering functionality.

Primarily, the issue is that data.gov.in does not follow a consistent date/time format in each of its datasets. A workaround that might help is to retrieve all of the data, and then filter the downloaded dataset for the date you want.

For example -

from datagovindia import DataGovIndia

YOUR_API_KEY = "579b464db66ec23****************************************"

datagovin = DataGovIndia(YOUR_API_KEY)

# Out:
# Step (1/2) : API key is VALID                                             
#	You don't need to enter it again                          
# Step (2/2) : Latest API meta-data loaded! You may begin.  

datagovin.search_by_description("mandi")

# Out:
# 1 of 418 results for : `mandi`

# ==================================================================================

# Resource-ID:	9ef84268d588465aa308a864a43d0070

# Current Daily Price of Various Commodities from Various Markets (Mandi)

# ==================================================================================

data = datagovin.get_data("9ef84268d588465aa308a864a43d0070")

This by default downloads all of the data in a matter of seconds. This particular dataframe has a timestamp in - unix format. You can convert it into a regular python recognized datetime format as follows -

import datetime


def convert_timestamp(x):
    """
    Converts UNIX- timestamp to standard datetime format.
    """
    return datetime.datetime.utcfromtimestamp(int(x))

data.loc[:,'date'] = data.timestamp.apply(convert_timestamp)

# Check results - 

data.date.head()

# Out:
# 0   2021-05-24 08:35:03
# 1   2021-05-24 08:35:03
# 2   2021-05-24 08:35:03
# 3   2021-05-24 08:35:03
# 4   2021-05-24 08:35:03
# Name: date, dtype: datetime64[ns]

Hope this solves your problem. It would be easier for me to provide you with a workaround for your issue if you could share the resource-id (a key that looks like - "9ef84268d588465aa308a864a43d0070") of the resource you're looking at.