question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pd.Timedelta is not JSON serializable

See original GitHub issue

In:

td =  pd.timedelta_range(0,periods=3,freq='h')
alt.Chart(
    pd.DataFrame(dict(id=np.arange(td.size),timedelta = td))
).mark_bar(
).encode(
 x = 'timedelta',
 y ='id:O'
)

Out:

~/anaconda/anaconda/envs/rr_dev/lib/python3.5/json/encoder.py in default(self, o)
    177 
    178         """
--> 179         raise TypeError(repr(o) + " is not JSON serializable")
    180 
    181     def encode(self, o):

TypeError: Timedelta('0 days 00:00:00') is not JSON serializable

In:

print ('pandas',pd.__version__)
print ('altair',alt.__version__)

Out:

pandas 0.23.0
altair 2.1.0

Expected, but I’m not sure that is achievable without timeUnit="hoursminutes". visualization 17

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Jun 26, 2018

OK, I’ve added a better error in #974

0reactions
jonathangjertsencommented, Jan 11, 2021

For future reference, I ended up with a workaround like this:

def add_difference_column(data, column, difference_column):
    diffed = data[column].diff()

    # Special case for timestamps due to https://github.com/altair-viz/altair/issues/967
    if diffed.dtype.kind == "m":
        diffed = diffed.dt.total_seconds()

    data[difference_column] = diffed

add_difference_column(data, "temperature", "temperature_diff")
add_difference_column(data, "voltage", "voltage_diff")
add_difference_column(data, "time", "time_diff")
# etc

If Altair/Vega supported timedeltas out of the box, this could be reduced to

def add_difference_column(data, column, difference_column):
    data[difference_column] = data[column].diff()

(the helper function is not strictly needed in this reduced example, but in my actual usecase this is part of a more complex operation with some filtering and grouping before and after)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to overcome "datetime.datetime not JSON serializable"?
Every time JSON tries to convert a value it does not know how to convert it will call the function we passed to...
Read more >
TypeError: Object of type datetime is not JSON serializable
The Python "TypeError: Object of type datetime is not JSON serializable" occurs when we try to convert a datetime object to a JSON...
Read more >
Object of type datetime is not JSON serializable - CodeProject
Solution 2. Generally, using of json. dumps with a custom converter. By default, it converts everything it doesn't know to a string.
Read more >
How to serialize a datetime object as JSON using Python?
TypeError: datetime.datetime (...) is not JSON serializable. How can we fix this? In the first example we can see the problem:.
Read more >
Tableau and Python
... is not JSON serializable. the script is attached below: SCRIPT_REAL(". import quants_core. import numpy as np. import pandas as pd. from datetime...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found