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.

Cannot Animate Plot by pandas.Timestamp

See original GitHub issue

Attempting to animate a plot using a pandas Timestamp column results in an exception.

Using Python 3.6.8, plotly_express==0.4.1, plotly==4.1.0

Code to replicate:

import plotly_express as px
import pandas as pd

# create a dataframe with mock data
df = pd.DataFrame(
    [
        {"x": 1, "y": 1, "date": "2018-01-01"},
        {"x": 2, "y": 1, "date": "2018-01-02"},
        {"x": 3, "y": 1, "date": "2018-01-03"},
    ]
)
df["date"] = pd.to_datetime(df["date"])
df.head()

#       date | x | y
# 2018-01-01 | 1 | 1
# 2018-01-02 | 2 | 1
# 2018-01-03 | 3 | 1

# attempt to plot
px.scatter(df, x="x", y="y", animation_frame="date")

Exception & stack trace:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\jmccain\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\express\_chart_types.py", line 52, in scatter
    return make_figure(args=locals(), constructor=go.Scatter)
  File "C:\Users\jmccain\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\express\_core.py", line 874, in make_figure
    orders, sorted_group_names = get_orderings(args, grouper, grouped)
  File "C:\Users\jmccain\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\express\_core.py", line 859, in get_orderings
    key=lambda g: orders[col].index(g[i]) if g[i] in orders[col] else -1,
  File "C:\Users\jmccain\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\express\_core.py", line 859, in <lambda>
    key=lambda g: orders[col].index(g[i]) if g[i] in orders[col] else -1,
ValueError: Timestamp('2018-01-01 00:00:00') is not in list

I found the cause of the issue, in _core.py:847

Pandas .unique() method returns a NumPy array, and as given in the examples section, it will convert a pandas.Timestamp to a numpy.datetime64.

>>> pd.Series([pd.Timestamp('2016-01-01') for _ in range(3)]).unique()
array(['2016-01-01T00:00:00.000000000'], dtype='datetime64[ns]')

The uniques list defined at _core.py:847 uses the .unique() method, converting the series of pandas.Timestamp into an array of numpy.datetime64

uniques = args["data_frame"][col].unique()

This distinction is relevant on line 857:

group_names = sorted(
    group_names,
    key=lambda g: orders[col].index(g[i]) if g[i] in orders[col] else -1
)

Which causes the ValueError when .index() does not find a matching element due to this behavior of pandas.Timestamp and numpy.datetime64 seen here:

>>> pandas.Timestamp('2018-01-01 00:00:00') in [numpy.datetime64('2018-01-01T00:00:00.000000000')]
True
>>> [numpy.datetime64('2018-01-01T00:00:00.000000000')].index(pandas.Timestamp('2018-01-01 00:00:00'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Timestamp('2018-01-01 00:00:00') is not in list

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

23reactions
robroccommented, Apr 24, 2020

I have this problem too. A simpler workaround that worked for me is passing: animation_frame = df.datecolumn.astype(str)

11reactions
toshipscommented, Dec 17, 2019

I have the same issue

workaround that works for me: df_summary_time = df_summary_time.sort_values(by='timestamp') # sort rows based on timestamp

df_summary_time['timestamp'] = df_summary_time.timestamp.apply(lambda x: x.date()).apply(str) # convert timestamp to a string

Then in px.scatter use animation_frame="timestamp"

Read more comments on GitHub >

github_iconTop Results From Across the Web

plotly (px) animation_frame error with datetime not being ...
I want to make an animate bar graph similar to the following example by plotly: https://plotly.com/python/animations/.
Read more >
Time animation graph - Plotly Community Forum
I want to plot segment (x) and distance (y) with time animation ... the points in one timestamp with a line, But I...
Read more >
Pandas find overlapping time intervals
In this post, we'll be going through an example of resampling time series data using pandas. Jan 26, 2022 · Now coming back...
Read more >
Graph Sensor Data with Python and Matplotlib - Digikey
Within animate(), we collect the temperature data and append a timestamp, just like in the previous example. We also truncate both xs and...
Read more >
[Code]-Dash plotly problem with timestamp in slider-pandas
How to move the timestamp bounds for datetime in pandas (working with historical data)? · Animated 3D Surface Plots with Plotly · How...
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