<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Importing datetime column from pandas dataframe in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Importing-datetime-column-from-pandas-dataframe/m-p/819635#M99765</link>
    <description>&lt;P&gt;I have a pandas data frame which has been imported from a database. One column is in the datetime64[ns] type. I want to copy the dataframe into a JMP data table, however the datatime data is lost (missing values) when using the method in this post:&amp;nbsp;&lt;A href="https://community.jmp.com/t5/JMPer-Cable/New-in-JMP-18-Python-jmp-DataTable-and-pandas-DataFrame/ba-p/744680" target="_blank" rel="noopener"&gt;https://community.jmp.com/t5/JMPer-Cable/New-in-JMP-18-Python-jmp-DataTable-and-pandas-DataFrame/ba-p/744680&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example is below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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])&lt;/PRE&gt;&lt;P&gt;A couple of workarounds are to either change the datetime to a string then use Parse Date() in a new column, or concert the datetime to an integer, add the necessary offset and then reformat it in the JMP table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;// New column to parse the string format date
Data Table( "tb" ):Column 6 &amp;lt;&amp;lt; Input Format( "d/m/y h:m:s", 0 ) &amp;lt;&amp;lt;
Format( "d/m/y h:m:s", 22, 0 ) &amp;lt;&amp;lt; Set Formula( Parse Date( :time_str ) );

// Convert the epoch date format to human readable
Data Table( "tb" ):datetime_epoch &amp;lt;&amp;lt; Input Format( "d/m/y h:m:s", 0 ) &amp;lt;&amp;lt;
Format( "d/m/y h:m:s", 22, 0 );&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Both are OK, but require some work and leave some redundant columns. Is there a proper JMP way of doing this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using JMP 18.1.1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Nov 2024 16:29:33 GMT</pubDate>
    <dc:creator>matth1</dc:creator>
    <dc:date>2024-11-28T16:29:33Z</dc:date>
    <item>
      <title>Importing datetime column from pandas dataframe</title>
      <link>https://community.jmp.com/t5/Discussions/Importing-datetime-column-from-pandas-dataframe/m-p/819635#M99765</link>
      <description>&lt;P&gt;I have a pandas data frame which has been imported from a database. One column is in the datetime64[ns] type. I want to copy the dataframe into a JMP data table, however the datatime data is lost (missing values) when using the method in this post:&amp;nbsp;&lt;A href="https://community.jmp.com/t5/JMPer-Cable/New-in-JMP-18-Python-jmp-DataTable-and-pandas-DataFrame/ba-p/744680" target="_blank" rel="noopener"&gt;https://community.jmp.com/t5/JMPer-Cable/New-in-JMP-18-Python-jmp-DataTable-and-pandas-DataFrame/ba-p/744680&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example is below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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])&lt;/PRE&gt;&lt;P&gt;A couple of workarounds are to either change the datetime to a string then use Parse Date() in a new column, or concert the datetime to an integer, add the necessary offset and then reformat it in the JMP table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;// New column to parse the string format date
Data Table( "tb" ):Column 6 &amp;lt;&amp;lt; Input Format( "d/m/y h:m:s", 0 ) &amp;lt;&amp;lt;
Format( "d/m/y h:m:s", 22, 0 ) &amp;lt;&amp;lt; Set Formula( Parse Date( :time_str ) );

// Convert the epoch date format to human readable
Data Table( "tb" ):datetime_epoch &amp;lt;&amp;lt; Input Format( "d/m/y h:m:s", 0 ) &amp;lt;&amp;lt;
Format( "d/m/y h:m:s", 22, 0 );&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Both are OK, but require some work and leave some redundant columns. Is there a proper JMP way of doing this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using JMP 18.1.1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2024 16:29:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Importing-datetime-column-from-pandas-dataframe/m-p/819635#M99765</guid>
      <dc:creator>matth1</dc:creator>
      <dc:date>2024-11-28T16:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: Importing datetime column from pandas dataframe</title>
      <link>https://community.jmp.com/t5/Discussions/Importing-datetime-column-from-pandas-dataframe/m-p/819661#M99770</link>
      <description>&lt;P&gt;I think both of those methods are ok.JMP doesn't recognize the timestamp which I think you did notice)&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_1-1732816645626.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/70718iC86434B4FC696FA4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_1-1732816645626.png" alt="jthi_1-1732816645626.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You can also do direct conversion (I think this might use temporary .csv)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; new data view;
	Python Send(dt);
"""
)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2024 17:58:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Importing-datetime-column-from-pandas-dataframe/m-p/819661#M99770</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-11-28T17:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: Importing datetime column from pandas dataframe</title>
      <link>https://community.jmp.com/t5/Discussions/Importing-datetime-column-from-pandas-dataframe/m-p/819752#M99786</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;. The Python Get() method does seem to save a temporary .csv file. I'll use the workarounds for now, but it would be nice to have a more efficient built-in way of copying pandas dataframes to JMP tables.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2024 09:25:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Importing-datetime-column-from-pandas-dataframe/m-p/819752#M99786</guid>
      <dc:creator>matth1</dc:creator>
      <dc:date>2024-11-29T09:25:34Z</dc:date>
    </item>
  </channel>
</rss>

