Massage input for bar traces for px.timeline()
(args)
| 2142 | |
| 2143 | |
| 2144 | def process_dataframe_timeline(args): |
| 2145 | """ |
| 2146 | Massage input for bar traces for px.timeline() |
| 2147 | """ |
| 2148 | args["is_timeline"] = True |
| 2149 | if args["x_start"] is None or args["x_end"] is None: |
| 2150 | raise ValueError("Both x_start and x_end are required") |
| 2151 | |
| 2152 | df: nw.DataFrame = args["data_frame"] |
| 2153 | schema = df.schema |
| 2154 | to_convert_to_datetime = [ |
| 2155 | col |
| 2156 | for col in [args["x_start"], args["x_end"]] |
| 2157 | if schema[col] != nw.Datetime and schema[col] != nw.Date |
| 2158 | ] |
| 2159 | |
| 2160 | if to_convert_to_datetime: |
| 2161 | try: |
| 2162 | df = df.with_columns(nw.col(to_convert_to_datetime).str.to_datetime()) |
| 2163 | except Exception as exc: |
| 2164 | raise TypeError( |
| 2165 | "Both x_start and x_end must refer to data convertible to datetimes." |
| 2166 | ) from exc |
| 2167 | |
| 2168 | # note that we are not adding any columns to the data frame here, so no risk of overwrite |
| 2169 | args["data_frame"] = df.with_columns( |
| 2170 | (nw.col(args["x_end"]) - nw.col(args["x_start"])) |
| 2171 | .dt.total_milliseconds() |
| 2172 | .alias(args["x_end"]) |
| 2173 | ) |
| 2174 | args["x"] = args["x_end"] |
| 2175 | args["base"] = args["x_start"] |
| 2176 | del args["x_start"], args["x_end"] |
| 2177 | return args |
| 2178 | |
| 2179 | |
| 2180 | def process_dataframe_pie(args, trace_patch): |