streamlit: Error: Unrecognized type: "Duration" (18)

Summary

The dataframe shows an error on the frontend if a duration type (timedelta) is used.

Steps to reproduce

Open in Streamlit Cloud

import pandas as pd
import streamlit as st

s = pd.Series(pd.date_range("2012-1-1", periods=3, freq="D"))

td = pd.Series([pd.Timedelta(days=i) for i in range(3)])

df = pd.DataFrame(
    {
        "A": s,
        "B": td
    }
)

st.table(df)
at Ji (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3064094)
at Function.$n.decode (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3065289)
at Ki (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3062354)
at Function.Yn.decode (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3066440)
at e._createHeader (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3059955)
at e.value (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3059100)
at e.value (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3069443)
at n.value (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3130013)
at n.value (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3125043)
at Function.value (http://localhost:8501/static/js/5.cbb83f60.chunk.js:2:3119592)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 7
  • Comments: 22 (1 by maintainers)

Most upvoted comments

Any update here? Currently running into this problem…

Still hoping for a fix to this issue…

Same problem. Temporarily working with:

st.code(df)

Yes, running into this issue as well.

I also did a quick check today into this issue. The duration type is only partially implemented in the javascript/typescript version of Arrow. For example, it is missing here: https://github.com/apache/arrow/blob/f5166fe21969d19adff23fc840ed1d7511348bad/js/src/ipc/metadata/message.ts#L440 I assume it just never got fully implemented yet. Feel free to leave an upvote on this Jira Issue to get some more attention on this.

In the meantime, a workaround would be either to use legacy serialization as described here or convert the timedelta value to int via df["delta"].dt.days or df["delta"].dt.seconds. For example:

import pandas as pd
import streamlit as st

data = {
    "dt1": ["2020-01-03 12:23", "2020-04-12 12:34"],
    "dt2": ["2021-03-23 12:13", "2021-12-12 11:04"],
}

df = pd.DataFrame(data)
df["dt1"] = pd.to_datetime(df["dt1"])
df["dt2"] = pd.to_datetime(df["dt2"])
df["delta"] = df["dt2"] - df["dt1"]

# Convert to integer value:
df["delta"] = df["delta"].dt.days

st.dataframe(df)

Thank you @felp99 and @david-huck !

We were able to reproduce the issue and can confirm that the bug exists.

The minimal code snippet that I used for verification is this:

import pandas as pd
import streamlit as st

s = pd.Series(pd.date_range("2012-1-1", periods=3, freq="D"))

td = pd.Series([pd.Timedelta(days=i) for i in range(3)])

df = pd.DataFrame(
    {
        "A": s,
        "B": td
    }
)

st.table(df)

As a workaround for now you can use

dataFrameSerialization = "legacy"

config option to switch to a legacy way of data frame serialization. I checked it and with dataFrameSerialization = "legacy" it works for time delta serialization.

Please see https://docs.streamlit.io/library/advanced-features/configuration for more information

@kmcgrady Yes, the new ArrowJS release finally provides support for the Duration type 🎉 However, we still have to do a few things on our side to actually make it work. I’m implementing the changes here: https://github.com/streamlit/streamlit/pull/7689

I’ve had the same issue when trying to use


st.write(df.dtypes.astype(str))
st.dataframe(df)

where the first line prints (among other things

...
Duration       timedelta64[ns]
...

And the error message shown in streamlit is:

Error: Unrecognized type: "Duration" (18)

$n.decode@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3067053
Ki@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3064118
Yn.decode@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3068206
value/a._createHeader@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3061719
value@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3060864
value@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3071207
value@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3131777
value@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3126807
value@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3121356
e@http://192.168.0.102:8501/static/js/main.3fe58c98.chunk.js:2:10639
get@http://192.168.0.102:8501/static/js/main.3fe58c98.chunk.js:2:425090
ln@http://192.168.0.102:8501/static/js/main.3fe58c98.chunk.js:2:457494
ia@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3254431
Gs@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3306889
Ml@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3294065
Cl@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3293993
Sl@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3293854
gl@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3290820
qi/<@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3240199
t.unstable_runWithPriority@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3317618
Ui@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3239976
qi@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3240146
Gi@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3240079
dl@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3288189
enqueueSetState@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3244097
g.prototype.setState@http://192.168.0.102:8501/static/js/5.c7ec7886.chunk.js:2:3190437
3156/n/r.handleDeltaMsg/<@http://192.168.0.102:8501/static/js/main.3fe58c98.chunk.js:2:558852

A naive approach, renaming the column, did not help…

I added timezone to my dates which works! to get around this error for displaying timedeltas

est = pytz.timezone(“America/New_York”) s = datetime.datetime.now().astimezone(est) e = datetime.datetime.now().astimezone(est) delta = (e-s)