Ho un data frame pandas che è stato importato da un database. Una colonna è nel tipo datetime64[ns]. Voglio copiare il dataframe in una tabella dati JMP, tuttavia i dati datatime vengono persi (valori mancanti) quando si utilizza il metodo in questo post: https://community.jmp.com/t5/JMPer-Cable/New-in-JMP-18-Python-jmp-DataTable-and-pandas-DataFrame/ba-p/744680
Di seguito un esempio:
import jmp
import pandas as pd
from pandas.api.types import is_object_dtype, is_numeric_dtype, is_bool_dtype, is_string_dtype, is_datetime64_dtype
df = pd.DataFrame({'txt': ['A', 'B'],
'num': [ 123, 543 ],
'time': [pd.to_datetime('2024-11-24 12:34:56'), pd.to_datetime('2024-11-25 12:34:56')]})
# ------- WORKAROUNDS BELOW -------
# Creating two new columns to get the datetime column to JMP data table
# Convert datetime to epoch and add Num( 01Jan1970 ) to match JMP's method:
df['datetime_epoch'] = df['time'].astype('int64')//1e9 + 2082844800
# or... create new column with datetime as a string
df['time_str'] = df[ 'time' ].astype('str')
# -------
dt0 = jmp.DataTable( 'tb', df.shape[0] )
# get the column names from the data frame
names = list(df.columns)
for j in range( df.shape[1] ):
# check if the coulumn data type is string or numeric
if is_string_dtype(df[ names[j] ] ):
dt0.new_column(names[j], jmp.DataType.Character )
else:
dt0.new_column(names[j], jmp.DataType.Numeric )
# populate the JMP column with data
dt0[j] = list(df.iloc[:,j])Un paio di soluzioni alternative consistono nel modificare la data e l'ora in una stringa e quindi utilizzare Parse Date() in una nuova colonna, oppure convertire la data e l'ora in un numero intero, aggiungere l'offset necessario e quindi riformattarlo nella tabella JMP.
// New column to parse the string format date
Data Table( "tb" ):Column 6 << Input Format( "d/m/y h:m:s", 0 ) <<
Format( "d/m/y h:m:s", 22, 0 ) << Set Formula( Parse Date( :time_str ) );
// Convert the epoch date format to human readable
Data Table( "tb" ):datetime_epoch << Input Format( "d/m/y h:m:s", 0 ) <<
Format( "d/m/y h:m:s", 22, 0 );
Entrambe sono OK, ma richiedono un po' di lavoro e lasciano alcune colonne ridondanti. Esiste un modo corretto di JMP per farlo?
Sto utilizzando JMP 18.1.1.
Penso che entrambi i metodi siano corretti. JMP non riconosce la marca temporale, cosa che credo tu abbia notato)

Puoi anche effettuare una conversione diretta (penso che questo potrebbe utilizzare un file .csv temporaneo)
import jmp
import pandas as pd
from pandas.api.types import is_object_dtype, is_numeric_dtype, is_bool_dtype, is_string_dtype, is_datetime64_dtype
df = pd.DataFrame({'txt': ['A', 'B'],
'num': [ 123, 543 ],
'time': [pd.to_datetime('2024-11-24 12:34:56'), pd.to_datetime('2024-11-25 12:34:56')]})
jmp.run_jsl("""
dt = Python Get(df);
dt << new data view;
Python Send(dt);
"""
)
Grazie @jthi . Il metodo Python Get() sembra salvare un file .csv temporaneo. Per ora userò le soluzioni alternative, ma sarebbe bello avere un modo integrato più efficiente per copiare i dataframe pandas nelle tabelle JMP.