Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Date Conversion issue
#1
Hello All,

Disclaimer (Python Newbie):

No matter what I do I get the 'TypeError: Variable.dtype datetime64[ns] not supported' error. Even after conversion. The stored Proc, two columns are DATETIME.

import pandas as pd
import pyodbc
import xport
import xport.v56
import numpy as np
import traceback

# Trusted Connection to Named Instance
try:
    connection = pyodbc.connect('DRIVER={SQL Server};SERVER=MyServer;DATABASE=myDB;USERID=MyUserID;PASSWORD=MyPassword;')

except Exception as err:
    print('Exception occured while trying to create a connection ', err)
else:
 

        sql_query = pd.read_sql_query('''EXEC SP.GetData @ID=0''', connection) 
        # here, the 'connection' is the variable that contains your database connection information from step 2

        df = pd.DataFrame(sql_query)

        df['dttmupd'] = pd.to_datetime(df['dttmupd'])
        df['dtiden'] = pd.to_datetime(df['dtiden'])

        ds = xport.Dataset(df, name='DATA', dataset_label='Wonderful Data')

        ds = df.rename(columns={k: k.upper()[:8] for k in ds})

        # Other SAS metadata can be set on the columns themselves.
        for k, v in ds.items():
            v.label = k.title()
           
            
            if v.dtype == 'object':
                v.format = '$CHAR20.'
            else:
                v.format = '10.2'
           
         
        # Libraries can have multiple datasets.
        
        
        df.info()
        library = xport.Library({'DATA': ds})

finally:
    connection.close()
Output:
C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\xport\v56.py:635: UserWarning: Converting column dtypes {'SPONNAM': 'string', 'SPONID': 'float', 'PROJNAM': 'string', 'PROJID': 'float', 'STUDNAM': 'string', 'STUDNO': 'string', 'STUDID': 'float', 'SITNAM': 'string', 'SITNNO': 'string', 'SITID': 'float', 'COUNTRY': 'string', 'CASEID': 'float', 'CASENAM': 'string', 'VERSNO': 'float', 'SUBJNO': 'string', 'CASESTAT': 'string', 'PANNAM': 'string', 'PANID': 'float', 'DSETNAM': 'string', 'DSETID': 'float', 'TYPEV': 'string', 'ISFATEV': 'string', 'DTIDEN': 'string', 'CASEPRTY': 'string', 'ISLOCKED': 'string', 'DISTRUL': 'string', 'PTPROFST': 'string', 'UPDNAM': 'string', 'DTTMUPD': 'string', 'CHGREAS': 'string', 'SPONCAS': 'string', 'DSETVER': 'float'} warnings.warn(f'Converting column dtypes {conversions}') Converting column 'SPONNAM' from object to string Converting column 'SPONID' from int64 to float Converting column 'PROJNAM' from object to string Converting column 'PROJID' from int64 to float Converting column 'STUDNAM' from object to string Converting column 'STUDNO' from object to string Converting column 'STUDID' from int64 to float Converting column 'SITNNO' from object to string Converting column 'SITID' from int64 to float Converting column 'COUNTRY' from object to string Converting column 'CASEID' from int64 to float Converting column 'CASENAM' from object to string Converting column 'VERSNO' from int64 to float Converting column 'SUBJNO' from object to string Converting column 'CASESTAT' from object to string Converting column 'PANNAM' from object to string Converting column 'PANID' from int64 to float Converting column 'DSETNAM' from object to string Converting column 'DSETID' from int64 to float Converting column 'TYPEV' from object to string Converting column 'ISFATEV' from object to string Converting column 'CASEPRTY' from object to string Converting column 'ISLOCKED' from object to string Converting column 'DISTRUL' from object to string Converting column 'PTPROFST' from object to string Converting column 'UPDNAM' from object to string Converting column 'CHGREAS' from object to string Converting column 'SPONCAS' from object to string Converting column 'DSETVER' from int64 to float PS C:\Users\Desktop\Test Files> python -u "c:\Users\Inc\Desktop\Test Files\xport_file.py" <class 'pandas.core.frame.DataFrame'> RangeIndex: 241 entries, 0 to 240 Data columns (total 32 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 sponnam 241 non-null object 1 sponid 241 non-null int64 2 projnam 241 non-null object 3 projid 241 non-null int64 4 studnam 241 non-null object 5 studno 241 non-null object 6 studid 241 non-null int64 7 sitnam 241 non-null object 8 sitnno 241 non-null object 9 sitid 241 non-null int64 10 country 241 non-null object 11 caseid 241 non-null int64 12 casenam 241 non-null object 13 versno 241 non-null int64 14 subjno 241 non-null object 15 casestat 241 non-null object 16 pannam 241 non-null object 17 panid 241 non-null int64 18 dsetnam 241 non-null object 19 dsetid 241 non-null int64 20 typev 217 non-null object 21 isfatev 208 non-null object 22 dtiden 206 non-null datetime64[ns] 23 caseprty 241 non-null object 24 islocked 241 non-null object 25 distrul 241 non-null object 26 ptprofst 117 non-null object 27 updnam 241 non-null object 28 dttmupd 241 non-null datetime64[ns] 29 chgreas 241 non-null object 30 sponcas 66 non-null object 31 dsetver 241 non-null int64 dtypes: datetime64[ns](1), int64(9), object(22) memory usage: 60.4+ KB
Error:
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\xport\v56.py", line 731, in __bytes__ return self._bytes() File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\xport\v56.py", line 739, in _bytes b'members': b''.join(bytes(Member(member)) for member in self.values()), File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\xport\v56.py", line 739, in <genexpr> b'members': b''.join(bytes(Member(member)) for member in self.values()), File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\xport\v56.py", line 618, in __bytes__ return self._bytes() File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\xport\v56.py", line 647, in _bytes header = bytes(MemberHeader.from_dataset(self)) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\xport\v56.py", line 358, in from_dataset ns = Namestr.from_variable(v, number=i) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\xport\v56.py", line 122, in from_variable raise TypeError(f'{type(variable).__name__}.dtype {variable.dtype} not supported') TypeError: Variable.dtype datetime64[ns] not supported
Reply
#2
Is that the complete and unaltered error traceback? Looks to be missing part, and all is important for debugging.
Reply
#3
This is it, should I try to produce more info add something to the code?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 281 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 672 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format and past date check function Turtle 5 4,338 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,262 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 2,913 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Type conversion issue while using Descriptor in Python3 mailnsuresh 1 2,892 May-07-2020, 12:26 PM
Last Post: deanhystad
  Substracting today's date from a date in column of dates to get an integer value firebird 1 2,146 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt
  Date format issue the_dude 11 7,625 Jun-07-2019, 06:27 AM
Last Post: snippsat
  How to change existing date to current date in a filename? shankar455 1 2,320 Apr-17-2019, 01:53 PM
Last Post: snippsat
  httplib to http.client conversion issue zatlas1 1 2,612 Apr-12-2019, 05:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020