Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging does not log pd.info()

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?

like image 404
loadbox Avatar asked May 21 '26 01:05

loadbox


1 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())
like image 120
jezrael Avatar answered May 22 '26 15:05

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!