Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] "Date" must be type "object" #512

Open
kampai-shp opened this issue Jan 31, 2025 · 0 comments
Open

[BUG] "Date" must be type "object" #512

kampai-shp opened this issue Jan 31, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@kampai-shp
Copy link

Expected Behavior

I was able to use the examples to get daily OHLCV data to work, but could not intraday data to chart. After a couple of days of going through code and testing various scenarios I think I got to the core issue. "Date" must be of type "object".

Problem arises using Polars to read a text file in CSV format. Pandas used to support "infer_datetime_format" but this was deprecated in version 2.0.0. Polars has "try_parse_dates" which returns a datetime64[us] type for intraday data. This type is most common for intraday analysis of stock prices.

So inputting data in Polars, then applying "to_pandas()" results in a datetime64[us] "Date" format, which fails in lightweight_charts. Applying:
df_polars["Date"] = df_polars["Date"].astype("object") works.

NOTE: This is not a "Polars problem". The issue is lightweight_charts not handling ["Date"] inputs that are not of type "object". There is code in _df_datetime_format in abstract/SeriesCommon that manipulates the Date or Time column names so I am using "Date" as the generic name.

Current Behaviour

The examples for daily & intraday (examples 1 & 3) provide an Excel .csv file. When this is read by Pandas, the type for the datetime is "object". This works, as referenced by running the examples. A simple "print(df.dtypes)" shows the data type.

Running the attached CSV-formatted text file in the reproducible example below shows how lightweight charts fails if the datetime format is not "object".

MP_test_data.txt

I believe the issue may be in abstract/SeriesCommon:
def _set_interval(self, df: pd.DataFrame):
if not pd.api.types.is_datetime64_any_dtype(df['time']):
df['time'] = pd.to_datetime(df['time'])

_set_interval wants an object type and if given another type, it skips this, which causes downstream issues.

Reproducible Example

import pandas as pd
import polars as pl
from lightweight_charts import Chart

if __name__ == '__main__':

    chart = Chart()

    df_pandas = pd.read_csv("MP_test_data.txt", header=None, names=["Date", "Open", "High", "Low", "Close", "Volume"])
    # infer_datetime_format was deprecated in Pandas 2.0.0
    print(df_pandas)
    print(df_pandas.dtypes) # Date is object

    df_polars = pl.read_csv(
        "MP_test_data.txt",
        has_header=False,
        new_columns=["Date", "Open", "High", "Low", "Close", "Volume"],
        try_parse_dates=True,
    ).to_pandas()
    print(df_polars)
    print(df_polars.dtypes) # Date is datetime64[us]

    # uncomment to make polars version work
    # df_polars["Date"] = df_polars["Date"].astype("object")

    # uncomment to show pandas works
    chart.set(df_pandas)

    # polars path - fails unless line above is uncommented
    # chart.set(df_polars)

    chart.show(block=True)

Environment

- OS: Windows 11
- Library: 2.1
@kampai-shp kampai-shp added the bug Something isn't working label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant