Open davidgilbertson opened 1 year ago
Thansk I can replicate this issue, basically it comes down to df.to_json())
failing in Python.
Note: for internal use, Data Wranger works here.
Cool, thanks for that. FYI I've added a comment here in the Pandas repo, not sure if it's on their radar.
One fix might be to be to convert all periods to date time, but you'd need to handle the case of a multi-index where only one of the indexes is Period (my use case and a common-ish format in hierarchical time series analysis).
With very little testing, this seems to work...
import pandas as pd
df = pd.DataFrame(
dict(
A=[1, 2, 12],
B=[1, 2, 12],
C=[1, 2, 12],
Date1=pd.period_range("2000-01-01", periods=3, freq="A"),
Date2=pd.period_range("2000-01-01", periods=3),
Date3=pd.date_range("2000-01-01", periods=3),
)
).set_index(["A", "Date1"])
def periods_to_timestamps(df):
new_df = df.copy()
for col_name, col in df.items():
if pd.api.types.is_period_dtype(col):
new_df[col_name] = col.dt.to_timestamp()
for level in range(df.index.nlevels):
index = df.index.get_level_values(level)
if pd.api.types.is_period_dtype(index):
new_df.index = new_df.index.set_levels(index.to_timestamp(), level=level)
return new_df
df2 = periods_to_timestamps(df)
Environment data
I'm using Pandas
2.0.3
. This happens in a WSL setup as well as just running in Windows.I didn't explicitly install Jupyter or IPython, I just let VS Code do whatever it had to do (install i-something-kernel to run the interactive window).
Expected behaviour
I should be able to view a DataFrame with the
Period
type in it (column or index).Actual behaviour
The data viewer crashes
Steps to reproduce:
venv
environment, installpandas
.test.py
with the contents:df = pd.DataFrame( dict( A=[1, 2, 3], Date=pd.period_range("2000-01-01", periods=3), ) )
Visual Studio Code (1.80.2, wsl, desktop) Jupyter Extension Version: 2023.6.1101941928. Python Extension Version: 2023.12.0. Platform: linux (x64). Workspace folder /mnt/c/Users/david/py/test3, Home = /home/davidg 15:19:20.274 [info] User belongs to experiment group 'FastKernelPicker' 15:19:20.274 [info] User belongs to experiment group 'NewRemoteUriStorage' 15:19:20.274 [info] User belongs to experiment group 'PasswordManager' 15:19:20.391 [info] Start refreshing Kernel Picker (1691039960391) 15:19:20.398 [info] Using Pylance 15:19:20.809 [info] Start refreshing Interpreter Kernel Picker 15:19:21.042 [info] Loading webview. View is notset 15:19:21.048 [info] Loading web view... 15:19:21.051 [info] Webview panel created. 15:19:21.553 [info] Web view react rendered 15:19:21.973 [info] Process Execution: /mnt/c/Users/david/py/test3/test3/bin/python -m pip list 15:19:23.191 [info] End refreshing Kernel Picker (1691039960391) 15:19:55.951 [info] Starting Kernel startUsingPythonInterpreter, .jvsc74a57bd02566ab5dbfd06e0d22b37f5de0f2fde6a8aff4465388f8f45199a2a85efd0a80./mnt/c/Users/david/py/test3/test3/python./mnt/c/Users/david/py/test3/test3/python.-m#ipykernel_launcher (Python Path: /mnt/c/Users/david/py/test3/test3/bin/python, Venv, test3, 3.11.2) for '/Interactive-2.interactive' (disableUI=false) 15:19:55.972 [info] Process Execution: /mnt/c/Users/david/py/test3/test3/bin/python -c "import ipykernel; print(ipykernel.version); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.file)" 15:19:56.048 [info] Process Execution: /mnt/c/Users/david/py/test3/test3/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9003 --control=9001 --hb=9000 --Session.signature_scheme="hmac-sha256" --Session.key=b"49a4eeb6-753b-4dc0-a177-ab4fb519c177" --shell=9002 --transport="tcp" --iopub=9004 --f=~/.local/share/jupyter/runtime/kernel-v2-52220A4kl18ZNWbp.json