如果您想了解如何将pythondatetime.datetime转换为Excel序列号的相关知识,那么本文是一篇不可错过的文章,我们将对pythondatetime.datetime转str进行全面详
如果您想了解如何将python datetime.datetime转换为Excel序列号的相关知识,那么本文是一篇不可错过的文章,我们将对python datetime.datetime转str进行全面详尽的解释,并且为您提供关于datetime.datetime.strptime在Python 2.4.1中不存在、delphi – 如何将WMI DateTime转换为标准DateTime?、Python -将Pandas列转换为DateTime、python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)的有价值的信息。
本文目录一览:- 如何将python datetime.datetime转换为Excel序列号(python datetime.datetime转str)
- datetime.datetime.strptime在Python 2.4.1中不存在
- delphi – 如何将WMI DateTime转换为标准DateTime?
- Python -将Pandas列转换为DateTime
- python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)
如何将python datetime.datetime转换为Excel序列号(python datetime.datetime转str)
我需要将日期转换为Excel序列号,以用于编写的数据处理脚本。通过在OpenOffice Calc工作簿中使用日期,我可以推断出“ 1-Jan 1899
00:00:00”映射到数字零。
我编写了以下函数,以将python datetime对象转换为Excel序列号:
def excel_date(date1): temp=dt.datetime.strptime(''18990101'', ''%Y%m%d'') delta=date1-temp total_seconds = delta.days * 86400 + delta.seconds return total_seconds
但是,当我尝试一些示例日期时,数字与在Excel(以及OpenOffice Calc)中将日期格式设置为数字时得到的数字不同。例如,在Python中测试“
2009-03-20”可获得3478032000,而excel将序列号呈现为39892。
上面的公式有什么问题?
*注意:我使用的是Python 2.6.3,因此无法访问datetime.total_seconds()
答案1
小编典典Excel的“序列日期”格式似乎实际上是自1900-01-00以来的 天
数,其分数部分是一天的一小部分,基于http://www.cpearson.com/excel/datetime。
htm。(我猜该日期实际上应该被认为是1899-12-31,因为不存在一个月的第0天)
因此,似乎应该是:
def excel_date(date1): temp = dt.datetime(1899, 12, 30) # Note, not 31st Dec but 30th! delta = date1 - temp return float(delta.days) + (float(delta.seconds) / 86400)
datetime.datetime.strptime在Python 2.4.1中不存在
在某些情况下,我们的团队需要使用Python 2.4.1。在Python
2.4.1strptime
中的datetime.datetime
模块中不存在:
Python 2.4.1 (#65,Mar 30 2005,09:13:57) [MSC v.1310 32 bit (Intel)]
Type "help","copyright","credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.strptime
Traceback (most recent call last):
File "<string>",line 1,in <fragment>
AttributeError: type object 'datetime.datetime' has no attribute 'strptime'
与2.6相反:
Python 2.6.6 (r266:84297,Aug 24 2010,18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help","credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.strptime
<built-in method strptime of type object at 0x1E1EF898>
键入时,我在2.4.1的时间模块中找到了它:
Python 2.4.1 (#65,09:16:17) [MSC v.1310 32 bit (Intel)]
Type "help","credits" or "license" for more information.
>>> import time
>>> time.strptime
<built-in function strptime>
我认为它strptime
在某个时候动了吗?检查这种事情的最好方法是什么。我尝试查看python的发行历史,但找不到任何东西。
delphi – 如何将WMI DateTime转换为标准DateTime?
解决方法
Python -将Pandas列转换为DateTime
我在以字符串格式导入的pandas DataFrame中有一个字段。它应该是日期时间变量。如何将其转换为datetime列,然后根据日期进行过滤。
例:
- DataFrame Name: raw_data
- Column Name: Mycol
- Value Format in Column: ‘05SEP2014:00:00:00.000’
答案1
小编典典使用该to_datetime
函数,指定一种格式以匹配您的数据。
raw_data[''Mycol''] = pd.to_datetime(raw_data[''Mycol''], format=''%d%b%Y:%H:%M:%S.%f'')
python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)
Python 的情况
先来看看 date 类型吧!
In [1]: from datetime import date
In [2]: date.min
Out[2]: datetime.date(1, 1, 1)
In [3]: str(date.min)
Out[3]: ''0001-01-01''
In [4]: str(date.max)
Out[4]: ''9999-12-31''
可以看到 python 的 date
的取值范围是 0001-01-01
到 9999-12-31
再来看看 datetime 吧!
In [5]: from datetime import datetime
In [6]: str(datetime.min)
Out[6]: ''0001-01-01 00:00:00''
In [7]: str(datetime.max)
Out[7]: ''9999-12-31 23:59:59.999999''
可以看到 python 的 datetime
的取值范围是 0001-01-01 00:00:00
到 9999-12-31 23:59:59.999999
Mysql 的情况
来看看 mysql 的情况吧,下面的官方文档中的内容:
The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in ''YYYY-MM-DD'' format. The supported range is ''1000-01-01'' to ''9999-12-31''.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ''YYYY-MM-DD hh:mm:ss'' format. The supported range is ''1000-01-01 00:00:00'' to ''9999-12-31 23:59:59''.The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of ''1970-01-01 00:00:01'' UTC to ''2038-01-19 03:14:07'' UTC.
总结一下就是:
- date 的取值范围:
1000-01-01
到9999-12-31
- timestamp 的取值范围:
1970-01-01 00:00:01
到2038-01-19 03:14:07
- datetime 的取值范围:
1000-01-01 00:00:00
到9999-12-31 23:59:59
参考资料:mysql 文档
但是需要注意:
Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type (''0000-00-00'' or ''0000-00-00 00:00:00''), if the SQL mode permits this conversion. The precise behavior depends on which if any of strict SQL mode and the NO_ZERO_DATE SQL mode are enabled; see Section 5.1.10, “Server SQL Modes”.
还有这部分:
翻译成人话的意思,就是 myql 会把无效的 date 设为 0000-00-00
,无效的 datetime 和 timestamp 设为 0000-00-00 00:00:00
。
比如 2022-13-35
就是一个无效的 date
,当把这个值插入到 mysql 中的时候,开启了严格模式的 mysql 会报错,未开启严格模式的 mysql 会转为 0000-00-00
保存。
‼️ 所以我们使用 Python 从 mysql 读取时间类型的时候,一定要注意这个坑!因为 0000-00-00
没有办法转成 Python 的 date
类型,会报错的!‼️
顺便可以看看 Mysql 的 date、timestamp、datetime 各占用几个字节:
- date 类型 3 字节
- timestamp 类型 4 字节
- datetime 类型 8 字节
参考文章:Data Type Storage Requirements
关于 Mysql 插入的时间类型可以是 0000-00-00
这种格式的原因可以参考:MySQL Incorrect datetime value: ''0000-00-00 00:00:00''
在 Mysql8 中默认是不可以插入这种无效时间的
关于如何将python datetime.datetime转换为Excel序列号和python datetime.datetime转str的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于datetime.datetime.strptime在Python 2.4.1中不存在、delphi – 如何将WMI DateTime转换为标准DateTime?、Python -将Pandas列转换为DateTime、python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)等相关知识的信息别忘了在本站进行查找喔。
本文标签: