Downcast numerics
(data: pd.DataFrame)
| 32 | |
| 33 | |
| 34 | def downcast_numbers(data: pd.DataFrame): |
| 35 | """Downcast numerics""" |
| 36 | |
| 37 | def downcast_ints(ser: pd.Series) -> pd.Series: |
| 38 | try: |
| 39 | ser = pd.to_numeric(ser, downcast="signed") |
| 40 | ser = pd.to_numeric(ser, downcast="unsigned") |
| 41 | except Exception: |
| 42 | pass # catch failure on Int64Dtype |
| 43 | return ser |
| 44 | |
| 45 | # A result of downcast(timedelta64[ns]) is int <ns> and hard to understand. |
| 46 | # e.g.) 0 days 00:54:38.777572 -> 3278777572000 [ns] |
| 47 | df_num = data.select_dtypes("integer", exclude=["timedelta"]) # , pd.Int64Dtype]) |
| 48 | data[df_num.columns] = df_num.apply(downcast_ints) |
| 49 | |
| 50 | def downcast_floats(ser: pd.Series) -> pd.Series: |
| 51 | ser = pd.to_numeric(ser, downcast="float", errors="ignore") |
| 52 | return ser |
| 53 | |
| 54 | # float downcasting currently disabled - alters values (both float64 and int64) and rounds to 'inf' instead of erroring |
| 55 | # see https://github.com/pandas-dev/pandas/issues/19729 |
| 56 | # df_num = data.select_dtypes("floating") |
| 57 | # data[df_num.columns] = df_num.apply(downcast_floats) |
| 58 | |
| 59 | |
| 60 | def timedelta_to_str(df: pd.DataFrame): |