简体   繁体   English

如何将Pandas Datatime列从Pandas datetime64 [ns]转换为Pyodbc SQL_Timestamp

[英]How to convert Pandas DataFrame column from Pandas datetime64[ns] to Pyodbc SQL_Timestamp

I am trying to populate Pandas Dataframe into empty MS Access 2016 table via pyodbc. 我正在尝试通过pyodbc将Pandas Dataframe填充到空的 MS Access 2016表中。 I get the following error message when I try to insert Dataframes into Access: pyodbc.dataerror: ('22008', [ODBC Microsoft Access Driver]Datetime field overflow . 当我尝试将数据帧插入Access中时,出现以下错误消息: pyodbc.dataerror :(“ 22008”,[ODBC Microsoft Access驱动程序] Datetime字段溢出

Research showed that MS Access Date/Time datatypes correspond to ODBC SQL_TIMESTAMP datatypes. 研究表明,MS Access 日期/时间数据类型与ODBC SQL_TIMESTAMP数据类型相对应。

I tried the following to convert datetime64[ns] to SQL_TIMESTAMP: 我尝试了以下方法将datetime64 [ns]转换为SQL_TIMESTAMP:

import datetime
cursor.execute("INSERT sql statement...VALUES(?)", datetime.datetime(order_date))  

However, I get this error: TypeError: an integer is required (got type Timestamp). 但是,我收到此错误: TypeError:需要一个整数(类型为Timestamp)。

What do I need to do in order to successfully populate Pandas/Numpy's datetime64[ns] values into Access tables? 为了成功将Pandas / Numpy的datetime64 [ns]值填充到Access表中,我需要做什么? Do I need to convert them into SQL_TIMESTAMP and how? 我需要将它们转换为SQL_TIMESTAMP以及如何转换吗?


EDIT: I tried running Gord Thompson's solution below and I am running into this error: 编辑:我尝试在下面运行Gord Thompson的解决方案,并且遇到了此错误:

 import datetime 

dt = dt64_to_datetime(dt_ns)

>> AttributeError:'datetime' has no attribute 'utcfromtimestamp'

What is the reason behind this error? 此错误的原因是什么? (Tested on pyodbc 4.0.17, Python 3.6.2, MS Access 2016) (在pyodbc 4.0.17,Python 3.6.2,MS Access 2016上测试)

What do I need to do in order to successfully populate Pandas/Numpy's datetime64[ns] values into Access tables? 为了成功将Pandas / Numpy的datetime64 [ns]值填充到Access表中,我需要做什么? Do I need to convert them into SQL_TIMESTAMP and how? 我需要将它们转换为SQL_TIMESTAMP以及如何转换吗?

As illustrated in this excellent answer , you'll probably need to convert the numpy.datetime64 values to Python datetime values, perhaps like this: 这个出色的答案所示 ,您可能需要将numpy.datetime64值转换为Python datetime值,也许是这样的:

def dt64_to_datetime(dt64):
    if np.isnat(dt64):
        return None
    else:
        unix_epoch = np.datetime64(0, 's')
        one_second = np.timedelta64(1, 's')
        seconds_since_epoch = (dt64 - unix_epoch) / one_second
        return datetime.utcfromtimestamp(seconds_since_epoch)

Example usage: 用法示例:

dt_ns = np.datetime64('2017-10-24 05:34:20.123456').astype('datetime64[ns]')
print(repr(dt_ns))  # numpy.datetime64('2017-10-24T05:34:20.123456000')
print(f'dt_ns.dtype: {dt_ns.dtype}')  # dt_ns.dtype: datetime64[ns]
dt = dt64_to_datetime(dt_ns)
print(repr(dt))  # datetime.datetime(2017, 10, 24, 5, 34, 20, 123456)

sql = "UPDATE tablename SET datetimefield = ? WHERE id=1"
params = (dt,)
crsr.execute(sql, params)

(Tested with pyodbc 4.0.21 and Access 2010.) (已使用pyodbc 4.0.21和Access 2010测试。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 datetime64 [ns]转换为Pandas中的时间戳字符串 - datetime64[ns] to Timestamp String in Pandas 将 datetime64[ns, UTC] pandas 列转换为日期时间 - convert datetime64[ns, UTC] pandas column to datetime Pandas 一次将 datetime64 [ns] 列转换为 datetime64 [ns, UTC] 用于多列 - Pandas convert datetime64 [ns] columns to datetime64 [ns, UTC] for mutliple column at once 如何将包含数据和datetime64 [ns]的列表与带有datetime64 [ns]索引的熊猫数据框合并 - How to merge a list containing data and datetime64[ns] with a pandas dataframe with datetime64[ns] index 将 datetime64[ns] 列转换为 pandas 中的 DatetimeIndex - Convert datetime64[ns] column to DatetimeIndex in pandas 使用'datetime64 [ns]'格式从熊猫数据框中提取 - Using 'datetime64[ns]' format for extraction from pandas dataframe 从 pandas dataframe datetime64[ns] 索引中删除时间 - Remove time from pandas dataframe datetime64[ns] index 如何处理 Python Pandas DataFrame 的缺失值 datetime64[ns] dtype 列? - How to handle missing value datetime64[ns] dtype column for Python Pandas DataFrame? Python Pandas - 如何转换 datetime64[ns] 并仅从中获取小时和分钟? - Python Pandas - How to convert datetime64[ns] and get hours and minutes only from it? Pandas 数据帧类型 datetime64[ns] 在 Hive/Athena 中不起作用 - Pandas dataframe type datetime64[ns] is not working in Hive/Athena
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM