Validates the inputted dataframe or list
(df)
| 27 | |
| 28 | |
| 29 | def validate_gantt(df): |
| 30 | """ |
| 31 | Validates the inputted dataframe or list |
| 32 | """ |
| 33 | if pd and isinstance(df, pd.core.frame.DataFrame): |
| 34 | # validate that df has all the required keys |
| 35 | for key in REQUIRED_GANTT_KEYS: |
| 36 | if key not in df: |
| 37 | raise exceptions.PlotlyError( |
| 38 | "The columns in your dataframe must include the " |
| 39 | "following keys: {0}".format(", ".join(REQUIRED_GANTT_KEYS)) |
| 40 | ) |
| 41 | |
| 42 | columns = {key: df[key].values for key in df} |
| 43 | num_of_rows = len(df.index) |
| 44 | chart = [] |
| 45 | # Using only keys present in the DataFrame columns |
| 46 | keys = list(df.columns) |
| 47 | for index in range(num_of_rows): |
| 48 | task_dict = {key: columns[key][index] for key in keys} |
| 49 | chart.append(task_dict) |
| 50 | |
| 51 | return chart |
| 52 | |
| 53 | # validate if df is a list |
| 54 | if not isinstance(df, list): |
| 55 | raise exceptions.PlotlyError( |
| 56 | "You must input either a dataframe or a list of dictionaries." |
| 57 | ) |
| 58 | |
| 59 | # validate if df is empty |
| 60 | if len(df) <= 0: |
| 61 | raise exceptions.PlotlyError( |
| 62 | "Your list is empty. It must contain at least one dictionary." |
| 63 | ) |
| 64 | if not isinstance(df[0], dict): |
| 65 | raise exceptions.PlotlyError("Your list must only include dictionaries.") |
| 66 | return df |
| 67 | |
| 68 | |
| 69 | def gantt( |