我有一個從資料庫匯入的 pandas 資料框。其中一列的類型為 datetime64[ns]。我想將資料幀複製到 JMP 資料表中,但是使用本文中的方法時資料時間資料遺失(缺失值): https
/community.jmp.com/t5/JMPer-Cable/New-in- JMP- 18-Python-jmp-DataTable-and-pandas-DataFrame/ba-p/744680
範例如下:
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])有兩種解決方法是將日期時間變更為字串,然後在新欄位中使用 Parse Date(),或將日期時間調整為整數,新增必要的偏移量,然後在 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 );
兩者都可以,但需要一些工作並留下一些多餘的列。有沒有正確的 JMP 方法來執行此操作?
我正在使用 JMP 18.1.1。
我認為這兩種方法都可以。

您也可以直接轉換(我認為這可能使用臨時.csv)
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);
"""
)
謝謝@jthi 。 Python Get() 方法似乎確實保存了臨時 .csv 檔案。我現在將使用這些解決方法,但如果有一種更有效的內建方法將 pandas 資料幀複製到 JMP 表,那就太好了。