xoolive/traffic

problem plotting traffic data (dtypes timedelta64 vs float64)

Closed this issue · 2 comments

I'm using the following code to try and plot traffic from the state vector data than can be downloaded from OpenSky (https://opensky-network.org/data/datasets#d1)

from traffic.core import Traffic
import matplotlib.pyplot as plt

t = Traffic.from_file("data/states_2021-03-08-00.csv")

with plt.style.context("traffic"):
    ax = plt.gca()
    t.plot(ax)
    plt.show()

The code gives me the following error:

Traceback (most recent call last):
  ...
TypeError: The DTypes <class 'numpy.dtype[timedelta64]'> and <class 'numpy.dtype[float64]'> do not have a common DType. For example they cannot be stored in a single array unless the dtype is `object`.

Am I using the right way of doing this?

The issue is that the timestamp column needs to be of type timestamp.
It should work with the following workaround:

Traffic.from_file(...).assign(
    timestamp=lambda df: pd.to_datetime(
        df.timestamp,
        unit="s",
    ).dt.tz_localize("utc")
)

I will close the issue, feel free to reopen is the problem persists