草庐IT

datetime2

全部标签

python - 可以在 python 中创建没有日期的 datetime.date 对象吗?

我试图在Python中输入日期,但有时我不知道确切的日期或月份。所以我只想记录年份。我想做类似的事情:datetime.date(year=1940,month="0orNone",day="0orNone")是否有执行此操作的代码?或者,如果没有,您将如何处理这个问题? 最佳答案 不幸的是,你不能传递0因为没有月份0所以你会得到ValueError:monthmustbein1..12,您不能跳过月份或日期,因为两者都是必需的。如果您不知道确切的年份或月份,只需为月份和日期传递1,然后只保留年份部分。>>>d=datetime.d

python - 如何从 pandas.DatetimeIndex 转换为 numpy.datetime64?

如何将pandas.DatetimeIndex转换为numpy.datetime64?我得到:>>>type(df.index.to_datetime())Out[56]:pandas.tseries.index.DatetimeIndexnumpy.array(datetimeindex,dtype=numpy.datetime64)安全吗? 最佳答案 中的数据是datetime64dtype(准确地说是datetime64[ns])。只需获取索引的values属性即可。请注意,它将以纳秒为单位。

python - 比较 datetime.datetime 是否存在或无

我正在使用Python在GoogleAppEngine上运行一个小应用程序。在模型中,我有一个DateTimeProperty类型的属性,即datetime.datetime。创建时没有值(即“无”)。我想比较datetime.datetime是否为None,但我不能。ifobject.updated_dateisNoneorobject.updated_date>=past:object.updated_date=nowupdated_date和past都是datetime.datetime。我收到以下错误。TypeError:can'tcomparedatetime.datetim

python - 将 datetime64 列拆分为 pandas 数据框中的日期和时间列

如果我有一个第一列是datetime64列的数据框。如何将此列拆分为2个新列,一个日期列和一个时间列。到目前为止,这是我的数据和代码:DateTime,Actual,Consensus,Previous2014011013:30:00,74000,196000,2410002013120613:30:00,241000,180000,2000002013110813:30:00,200000,125000,1630002013102212:30:00,163000,180000,1930002013090612:30:00,193000,180000,1040002013080212:

python - 何时使用 datetime.utcnow() 或 datetime.now(tz=pytz.utc).replace(tzinfo=None)

我想知道什么时候应该使用datetime.now(tz=pytz.utc).replace(tzinfo=None)相对于简单datetime.utcnow()后者会不会考虑例如夏令时? 最佳答案 datetime.datetime的很多工作方式取决于运行它的机器。主机的本地时间和时区设置将决定您将获得的输出。如果主机处于UTC时区,则datetime.datetime.now()和datetime.datetime.utcnow()之间没有区别。根据pytzdocumentation:Thepreferredwayofdealin

python - 属性错误 : 'datetime.datetime' object has no attribute 'timestamp'

请帮忙-我不断收到以下回溯错误:当前运行Python2.0我正在尝试利用Python的Plotly库来显示说明比特币价格的信息图。我尝试在代码顶部导入日期时间,但这似乎无法解决问题。Traceback(mostrecentcalllast):File"project_one.py",line165,incrypto_price_df=get_crypto_data(coinpair)File"project_one.py",line155,inget_crypto_datajson_url=base_polo_url.format(poloniex_pair,start_date.ti

python - 如何将 datetime.date 对象转换为 time.struct_time 对象?

我有一个python脚本,我需要比较两个日期。我有一个日期列表作为time.struct_time对象,我需要将其与几个datetime.date对象进行比较。如何将datetime.date对象转换为time.struct_time对象?或者我可以直接使用它们进行比较吗? 最佳答案 尝试使用date.timetuple().来自Python文档:Returnatime.struct_timesuchasreturnedbytime.localtime().Thehours,minutesandsecondsare0,andtheD

python - 为什么python datetime类有 'fromtimestamp'方法,没有 'totimestamp'方法?

Python的datetime类有一个fromtimestamp方法来从时间戳创建一个datetime对象,但不提供totimestamp反过来的方法...我知道使用time.mktime(x.timetuple())之类的东西,您可以将datetime对象转换为时间戳,但这对我来说看起来没有必要复杂,所以我很好奇为什么没有totimestamp方法? 最佳答案 我确实记得一个discussion/bugreport关于这件事,而我前段时间对此感到疑惑。长话短说:已经提出了很多建议,但由于某种原因,没有一个被接受。重点是我认为最好总

python - 混合 datetime.strptime() 参数

混淆datetime.strptime()是一个很常见的错误格式化字符串和日期字符串参数使用:datetime.strptime("%B%d,%Y","January8,2014")而不是反过来:datetime.strptime("January8,2014","%B%d,%Y")当然,它会在运行时失败:>>>datetime.strptime("%B%d,%Y","January8,2014")Traceback(mostrecentcalllast):File"",line1,inFile"/System/Library/Frameworks/Python.framework/V

python - 将 zope DateTime 对象转换为 Python datetime 对象的最佳方法是什么?

我需要将zope2DateTime对象转换为Pythondatetime对象。最好的方法是什么?谢谢,埃里卡 最佳答案 较新的DateTime实现(2.11及更高版本)有一个返回pythondatetime.datetime实例的asdatetime方法:modernthingy=zopethingy.asdatetime() 关于python-将zopeDateTime对象转换为Pythondatetime对象的最佳方法是什么?,我们在StackOverflow上找到一个类似的问题: