import logging
import pandas as pd
logger = logging.getLogger('train')
logger.setLevel(logging.DEBUG)
# Data
data = {'Name': ['Tom', 'nick', 'krish', 'jack'], 'Age': [20, 21, 19, 18]}
# Create DataFrame
df = pd.DataFrame(data)
logger.info(type(df))
logger.info(df.info())
.
.
.
<other_processes>
.
The above code outputs:
<class 'pandas.core.frame.DataFrame'>
None
.
.
.
And at the end of the logs (after all other processes), it also outputs:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
Name 4 non-null object
Age 4 non-null int64
dtypes: int64(1), object(1)
memory usage: 144.0+ bytes
Why does it print None when I try to log df.info()? How can I get df.info() at the intended location in my logs?
Change buffer parameter in DataFrame.info to StringIO for text with .getvalue():
from io import StringIO
buf = StringIO()
df.info(buf=buf)
logger.info(type(df))
logger.info(buf.getvalue())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With