Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectByDate: Code Extension (refer issue #53) #57

Closed
wants to merge 8 commits into from

Conversation

Tanvi-Jain01
Copy link

This PR extends the code of Issue #53
@nipunbatra , @patel-zeel

BEFORE:

CODE:

df.index = pd.to_datetime(df.date)
df = df.drop("date", axis=1)
df_n = df[year].resample("1D").mean()
df_n = df_n.fillna(method="ffill")
df_n["month"] = df_n.index.month
df_n.index.dayofweek
print(df_n)

AFTER:

CODE:

import pandas as pd
    
    df['date'] = pd.to_datetime(df['date'])
    df_year = df[df['date'].dt.year == int(year)]
    
    if group:
        df_grouped = df_year.groupby(group).resample(time_period[0], on='date').mean(numeric_only=True)
        return df_grouped
    
    if time_period == 'month':
        df_month = df_year.resample('M', on='date').mean(numeric_only=True)
        return df_month
    elif time_period == 'year':
        df_yearly = df_year.resample('Y', on='date').mean(numeric_only=True)
        return df_yearly
    
    df_day = df_year.resample('D', on='date').mean(numeric_only=True)
    return df_day

USAGE:

selectByDate(df1,'2022',group=['latitude','longitude','station'], time_period='month')

Additional Time Periods: The modified function introduces the capability to compute the average value of each month or year in addition to daily averages. This provides more granular insights into the data.

Grouping Support: The modified function allows for optional grouping of the data by specified columns. This enables the calculation of average values based on different groups, providing more customized analysis and comparisons.

Resampling Flexibility: The modified function uses the resample method with dynamic frequency parameters based on the selected time period. This allows for greater flexibility in computing average values at different frequencies without hardcoding the resampling periods.

OUTPUT:
selectby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant