# --------------------------------------------------------------------------------------------# Copyright (c) Microsoft Corporation. All rights reserved.# Licensed under the MIT License. See LICENSE.txt in the project root for license information.# --------------------------------------------------------------------------------------------"""Takes a vector of dates and identify whether the frequency is 'daily', 'weekly', or 'monthly'. The primary use case for this function is to provide an accurate description of the query type used and for raising errors should a wrong date grouping be used in the data input."""importpandasaspd
[docs]defidentify_datefreq(x):x=pd.to_datetime(x)# Data frame for checkingdate_df=pd.DataFrame({"weekdays":pd.Series(list(pd.Series(x).dt.weekday.unique())),"n":pd.Series(list(pd.Series(x).dt.weekday.value_counts()))})iflen(pd.Series(x).dt.month_name().unique())==len(x):return"monthly"elifsum(date_df.loc[date_df.weekdays==6].n)==len(x):# Check number of Sundays - should equal number of weeks if weeklyreturn"weekly"eliflen(date_df)>=3:# At least 3 days of the week must be presentreturn"daily"else:return"Unable to identify date frequency."