我有一个从数据库导入的 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。
我认为这两种方法都可以。JMP 无法识别时间戳,我想你注意到了)

您也可以直接转换(我认为这可能会使用临时的 .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 表就更好了。