Last active
August 6, 2018 16:08
-
-
Save WillKoehrsen/7b63ca67304fbfdae8dad7c74164aa61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def total_previous_month(numeric, datetime, time): | |
"""Return total of `numeric` column in the month prior to `time`.""" | |
df = pd.DataFrame({'value': numeric, 'time': datetime}) | |
previous_month = time.month - 1 | |
# Handle January | |
if previous_month == 0: | |
previous_month = 12 | |
previous_year = time.year - 1 | |
# Filter data | |
df = df[(df['time'].dt.month == previous_month) & | |
(df['time'].dt.year == previous_year)] | |
else: | |
df = df[df['time'].dt.month == previous_month] | |
# Sum up total | |
total = df['value'].sum() | |
return total | |
# Define the custom primitive | |
total_previous = make_agg_primitive(total_previous_month, | |
input_types = [ft.variable_types.Numeric, | |
ft.variable_types.Datetime], | |
return_type = ft.variable_types.Numeric, | |
uses_calc_time = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment