You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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".
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
importpandasaspdimportpolarsasplfromlightweight_chartsimportChartif__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.0print(df_pandas)
print(df_pandas.dtypes) # Date is objectdf_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 workschart.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
The text was updated successfully, but these errors were encountered:
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
Environment
The text was updated successfully, but these errors were encountered: