Python logging does not log pd.info()

Viewed 1112
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?

3 Answers

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())

Using jezrael's solution I created a function that can be called whenever df.info() needs to be logged.

def log_table(df):
    buf = StringIO()
    df.info(buf=buf)
    logging.debug(type(df))
    logging.debug(buf.getvalue())

You can use f-strings

logging.info(f"{df.info()}")
Related