MCPcopy Create free account
hub / github.com/datapane/datapane / downcast_numbers

Function downcast_numbers

python-client/src/datapane/common/df_processor.py:34–57  ·  view source on GitHub ↗

Downcast numerics

(data: pd.DataFrame)

Source from the content-addressed store, hash-verified

32
33
34def 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
60def timedelta_to_str(df: pd.DataFrame):

Callers 2

test_downcast_numbersFunction · 0.90
process_dfFunction · 0.85

Calls 1

applyMethod · 0.45

Tested by 1

test_downcast_numbersFunction · 0.72