Efficient Data Compression Techniques in Pandas
Instruction: Discuss methods for compressing DataFrame memory usage without loss of information.
Context: This question assesses the candidate's knowledge of data compression techniques in Pandas, crucial for optimizing performance and resource usage.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Lossless memory reduction requires preserving values, missingness and relevant semantics. Measure actual memory before and after; storage-file compression and in-memory representation are different.
import pandas as pd import numpy as np values = pd.Series([1, 100, 127], dtype="int64") small = pd.to_numeric(values, downcast="integer") assert str(small.dtype) == "int8" assert small.astype("int64").equals(values) f = np.array([1.00000001], dtype=np.float64) assert not np.array_equal(f.astype(np.float32).astype(np.float64),...