草庐IT

DateTime-Local

全部标签

python - datetime.strptime() 抛出 'does not match format' 错误

我明白了timedata'19/Apr/2011:22:12:39'doesnotmatchformat'%d/%b/%y:%H:%M:%S'当使用datetime.strptime('19/Apr/2011:22:12:39','%d/%b/%y:%H:%M:%S')我做错了什么? 最佳答案 试试%d/%b/%Y:%H:%M:%S-%y现在表示11。您可以使用date轻松地“调试”日期时间格式(在shell而不是python上,我的意思是,假设您正在运行GNU/Linux或类似系统):date'+%d/%b/%Y:%H:%M:%S

python - pd.Timestamp 与 np.datetime64 : are they interchangeable for selected uses?

这个问题的动机是ananswer到questiononimprovingperformance在pandas中与DatetimeIndex进行比较时。该解决方案通过df.index.values将DatetimeIndex转换为numpy数组,并将该数组与np.datetime64对象。这似乎是从此比较中检索bool数组的最有效方法。pandas的一位开发人员对这个问题的反馈是:“这些通常不一样。提供numpy解决方案通常是一种特殊情况,不推荐使用。”我的问题是:对于一部分操作,它们是否可以互换?我很感激DatetimeIndex提供了更多功能,但我只需要基本功能,例如切片和索引。对于

python - "local variable referenced before assignment"——只有功能?

采用以下代码:importsomethingdefFoo():something=something.SomeClass()returnsomething...这显然不是有效代码:UnboundLocalError:localvariable'something'referencedbeforeassignment...因为局部变量something被创建,但没有赋值,在=的RHS被评估之前。(例如,请参见thisrelatedanswer'scomment。)这对我来说似乎有点奇怪,但可以肯定的是,我会接受它。现在,为什么下面的代码有效?classFoo(object):someth

python - SqlAlchemy 不接受 DateTime 列中的 datetime.datetime.now 值

我应该首先提到我通过Flask-SqlAlchemy使用SqlAlchemy。我认为这不会影响问题,但如果有影响,请告诉我。这是我在SqlAlchemy中运行create_all函数时收到的错误消息的相关部分InterfaceError:(InterfaceError)错误绑定(bind)参数4-可能是不支持的类型。u'INSERTINTOpodcasts(feed_url,title,url,last_updated,feed_data)VALUES(?,?,?,?,?)'(u'http://example.com/feed',u'PodcastShowTitle',u'http:/

python - 为什么找不到 Python datetime time delta?

我正在尝试以mmddyyyy格式制作一组日期。日期将从当天开始,然后到future两周。所以这一切都取决于开始日期。当我运行我的代码时,我收到一条错误消息:Traceback(mostrecentcalllast):File"timeTest.py",line8,inday=datetime.timedelta(days=i)AttributeError:typeobject'datetime.datetime'hasnoattribute'timedelta'我不确定为什么会这样,因为在网上搜索后,我注意到人们以这种方式使用“timedelta”。这是我的代码:importtimef

python - 为什么 datetime.now() 和 datetime.today() 在我的电脑上显示 UTC 时间而不是本地时间?

datetime.now()和datetime.today()在我的计算机上返回UTC时间,即使thedocumentation说它们应该返回本地时间。这是我运行的脚本:#!/usr/bin/pythonimporttimeimportdatetimeif__name__=="__main__":print(datetime.datetime.now())print(datetime.datetime.today())print(datetime.datetime.fromtimestamp(time.time()))这是输出:2017-11-2922:47:35.3399142017

python - 如何直接从 datetime.datetime 子模块导入 now()

背景:我在Python程序中有几个重复调用的紧密循环,其中包括datetime.datetime.now()方法,以及datetime.datetime.min和datetime.datetime.max属性。为了优化,我想将它们导入本地命名空间,避免重复的、不必要的模块层次结构名称查找,如下所示:fromdatetime.datetimeimportnow,min,max但是,Python会提示:Traceback(mostrecentcalllast):File"my_code.py",line1,infromdatetime.datetimeimportnow,min,maxIm

Python MySQLdb 返回 datetime.date 和 decimal

我有一个MySQL查询:SELECTmydate,countryCode,qtySoldfromsalesordermydate,countryCode这将返回具有如下值的元组的元组:((datetime.date(2011,1,3),'PR',Decimal('1')),(datetime.date(2011,1,31),'MX',Decimal('1')))当我尝试使用循环打印时,它打印得非常好:2011-1-3,PR,12011-1-31,MX,1但是当我尝试返回这个值时,它返回为datetime.date(2011,1,3),'PR',Decimal('1')有没有办法获取正常

python - 曲线拟合到格式为 'datetime' 的时间序列?

这是我的问题:polyfit不采用日期时间值,因此我使用mktime转换日期时间产生多项式拟合效果z4=polyfit(d,y,3)p4=poly1d(z4)然而,对于情节,我想要轴上的日期时间描述,但没有#弄清楚如何去做。你能帮帮我吗?fig=plt.figure(1)cx=fig.add_subplot(111)xx=linspace(0,d[3],100)pylab.plot(d,y,'+',xx,p4(xx),'-g')cx.plot(d,y,'+',color='b',label='blub')plt.errorbar(d,y,yerr,marker='.',color='k

python - 删除 dtype datetime NaT

我正在准备一个pandasdf用于输出,并想删除表中的NaN和NaT,并将这些表位置留空。一个例子是mydataframesamplecol1col2timestampab2014-08-14cNaNNaT会变成col1col2timestampab2014-08-14c大部分值是dtypes对象,时间戳列是datetime64[ns]。为了解决这个问题,我尝试使用panda的mydataframesample.fillna('')有效地在该位置留出空间。但是,这不适用于日期时间类型。为了解决这个问题,我尝试将时间戳列转换回对象或字符串类型。是否可以在不进行类型转换的情况下删除NaN/