MCPcopy
hub / github.com/pandas-dev/pandas / test_info_memory_usage

Function test_info_memory_usage

pandas/tests/frame/methods/test_info.py:318–400  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

316
317
318def test_info_memory_usage():
319 # Ensure memory usage is displayed, when asserted, on the last line
320 dtypes = [
321 "int64",
322 "float64",
323 "datetime64[ns]",
324 "timedelta64[ns]",
325 "complex128",
326 "object",
327 "bool",
328 ]
329 data = {}
330 n = 10
331 for i, dtype in enumerate(dtypes):
332 data[i] = np.random.default_rng(2).integers(2, size=n).astype(dtype)
333 df = DataFrame(data)
334 buf = StringIO()
335
336 # display memory usage case
337 df.info(buf=buf, memory_usage=True)
338 res = buf.getvalue().splitlines()
339 assert "memory usage: " in res[-1]
340
341 # do not display memory usage case
342 df.info(buf=buf, memory_usage=False)
343 res = buf.getvalue().splitlines()
344 assert "memory usage: " not in res[-1]
345
346 df.info(buf=buf, memory_usage=True)
347 res = buf.getvalue().splitlines()
348
349 # memory usage is a lower bound, so print it as XYZ+ MB
350 assert re.match(r"memory usage: [^+]+\+", res[-1])
351
352 df.iloc[:, :5].info(buf=buf, memory_usage=True)
353 res = buf.getvalue().splitlines()
354
355 # excluded column with object dtype, so estimate is accurate
356 assert not re.match(r"memory usage: [^+]+\+", res[-1])
357
358 # Test a DataFrame with duplicate columns
359 dtypes = ["int64", "int64", "int64", "float64"]
360 data = {}
361 n = 100
362 for i, dtype in enumerate(dtypes):
363 data[i] = np.random.default_rng(2).integers(2, size=n).astype(dtype)
364 df = DataFrame(data)
365 df.columns = dtypes
366
367 df_with_object_index = DataFrame({"a": [1]}, index=Index(["foo"], dtype=object))
368 df_with_object_index.info(buf=buf, memory_usage=True)
369 res = buf.getvalue().splitlines()
370 assert re.match(r"memory usage: [^+]+\+", res[-1])
371
372 df_with_object_index.info(buf=buf, memory_usage="deep")
373 res = buf.getvalue().splitlines()
374 assert re.match(r"memory usage: [^+]+$", res[-1])
375

Callers

nothing calls this directly

Calls 11

infoMethod · 0.95
memory_usageMethod · 0.95
DataFrameClass · 0.90
IndexClass · 0.90
matchMethod · 0.80
from_productMethod · 0.80
astypeMethod · 0.45
infoMethod · 0.45
sumMethod · 0.45
sizeMethod · 0.45
memory_usageMethod · 0.45

Tested by

no test coverage detected