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

Perspective does not show data if datetime column contains NaT #6162

Closed
MarcSkovMadsen opened this issue Jan 5, 2024 · 0 comments · Fixed by #6176
Closed

Perspective does not show data if datetime column contains NaT #6162

MarcSkovMadsen opened this issue Jan 5, 2024 · 0 comments · Fixed by #6176
Milestone

Comments

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Jan 5, 2024

I'm on Panel==1.3.6 trying to use Perspective.

import pandas as pd
import pandas as pd
import numpy as np

# Create a sample DataFrame with a datetime column
data = {'Datetime': pd.to_datetime(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04'])}
df = pd.DataFrame(data)

# Assign NaN values to specific rows or cells
df.loc[1, 'Datetime'] = np.nan
df.loc[3, 'Datetime'] = np.nan
print(df)
print(df.dtypes)

import panel as pn
pn.extension("perspective")

pn.pane.Perspective(df, sizing_mode="stretch_width").servable()

image

format_cell.js:71  Uncaught (in promise) RangeError: Invalid time value

Hacky Workaround

Replace the NaT value with a valid timestamp

import pandas as pd
import pandas as pd
import numpy as np

# Create a sample DataFrame with a datetime column
data = {'Datetime': pd.to_datetime(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04'])}
df = pd.DataFrame(data)

# Assign NaN values to specific rows or cells
df.loc[1, 'Datetime'] = np.NAN
df.loc[3, 'Datetime'] = np.NAN
print(df)
print(df.dtypes)

NAT_TIMESTAMP = pd.Timestamp('1980-01-01')

def _clean_for_perspective(data: pd.DataFrame):
    datetime_columns = data.select_dtypes(include='datetime64[ns]').columns
    data[datetime_columns] = data[datetime_columns].fillna(NAT_TIMESTAMP)
    return data

df = _clean_for_perspective(df)

import panel as pn
pn.extension("perspective")

pn.pane.Perspective(df, sizing_mode="stretch_width").servable()
@MarcSkovMadsen MarcSkovMadsen added this to the v1.3.7 milestone Jan 5, 2024
@MarcSkovMadsen MarcSkovMadsen changed the title Perspective does not show data if datetime column contains NaN Perspective does not show data if datetime column contains NaT Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant