Skip to content
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
This repository was archived by the owner on Aug 13, 2026. It is now read-only.

logging_v2 cannot log Pandas DataFrames #409

Closed

Description

@astanway

Logging

Environment details

  • OS type and version: ubuntu 18.04
  • Python version: 3.6.9
  • pip version: 18.1
  • Pandas version: 0.23.4
  • google-cloud-logging version: 2.6.0

Steps to reproduce

>>> import sys
>>> import logging
>>> from google.cloud.logging_v2.handlers import CloudLoggingHandler
>>> from google.cloud import logging_v2 as glog
>>> log = logging.getLogger()
>>> client = glog.Client()
>>> cloudHandler = CloudLoggingHandler(client, name='logger')
>>> handlers = [cloudHandler]
>>> logging.basicConfig(level=logging.INFO, handlers=handlers)
>>> import pandas as pd
>>> x = pd.DataFrame()
>>> log.info(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1308, in info
    self._log(INFO, msg, args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1444, in _log
    self.handle(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1454, in handle
    self.callHandlers(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1516, in callHandlers
    hdlr.handle(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 861, in handle
    rv = self.filter(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 720, in filter
    result = f.filter(record)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/logging_v2/handlers/handlers.py", line 107, in filter
    record._msg_str = str(record.msg).replace('"', '\\"') if record.msg else ""
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/generic.py", line 1576, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Activity

  1. daniel-sanche commented on Oct 4, 2021

    @daniel-sanche
    Contributor

    Duplicate of #345

    log.info(x) is part of the python logging standard library, which expects string inputs. You should turn the dataframe into a string before logging

    log.info(str(x))
    

    I'm going to keep this open to see if we can handle this better internally though, since it's impacting multiple users

  2. added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    priority: p3Desirable enhancement or fix. May not be included in next release.
    on Oct 4, 2021
  3. added this to the v3.0.0 Breaking milestone on Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: loggingIssues related to the googleapis/python-logging API.priority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions