在Python中,我将a转换date为datetime:
date为stringstring为datetime码:
import datetime
dt_format="%d%m%Y"
my_date = datetime.date.today()
datetime.datetime.strptime(my_date.strftime(dt_format), dt_format)
Run Code Online (Sandbox Code Playgroud)
我怀疑这远非最有效的方法.在Python中将日期转换为日期时间的最有效方法是什么?
我喜欢用dateutil.parser.parse模块解析日期时间字符串.这很简单.但是我在我的代码中注意到我必须检查对象是否确实在8601(并且知道).
我的结构是:
if parse(datetime).tzinfo==None:
#do something
else:
#make it aware
#do something
Run Code Online (Sandbox Code Playgroud)
我希望实现以下目标:
if <IS-8601>:
if parse(datetime).tzinfo==None:
#do something
else:
#make it aware
#do something
else:
pass
Run Code Online (Sandbox Code Playgroud)
如果我有一个8601像2014-02-28T22:30:00+0200解析实用程序做它的工作.
但是,如果我有一个2014-03-20字符串解析将在对象上添加时间.这没错,只是不需要的:2014-03-20 00:00:00
那么如何检查对象是否在8601中?如果在8601,它是否知道?我不介意换到另一个日期时间库.
>>> now = datetime.datetime.now(pytz.timezone('Asia/Tokyo'))
>>> dt = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, pytz.timezone('Asia/Tokyo'))
>>> now
datetime.datetime(2018, 9, 7, 16, 9, 24, 177751, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)
>>> dt = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, pytz.timezone('Asia/Tokyo'))
>>> dt
datetime.datetime(2018, 9, 7, 16, 9, 24, 177751, tzinfo=<DstTzInfo 'Asia/Tokyo' LMT+9:19:00 STD>)
Run Code Online (Sandbox Code Playgroud)
因为now我得到了JST + 9:00:00,因为dt我得到了LMT +9:19:00.我不明白为什么datetime使用不同的格式.
当我比较他们不同的时间时:
>>> now == dt
False
Run Code Online (Sandbox Code Playgroud)
我怎么能转换LMT到JST,这样now == dt …
我在 Python 程序中有一个以毫秒为单位的值。例如:1557975599999
我想将其转换为包含天、小时、分钟、秒的字符串。我怎样才能做到这一点?
我有以下格式的 Excel 表格:
我想使用pandas模块使用 Python 读取此表并计算发布日期和当前日期之间的差异。这是我当前的代码:
import pandas as pd
import datetime as dt
def abc():
a=pd.read_excel('date time.xlsx')
b=dt.date.today()
print(b)
c=(a['date of issue'])
h=(c[0])
f=dt.datetime(h)
d=b-f
print(d)
abc()
Run Code Online (Sandbox Code Playgroud)
它在第 7 行 ( f=dt.datetime(h)) 中显示错误。它读TypeError: an integer is required (got type Timestamp)。
我有一个数据框,是我在 pandas 中创建的,按日期分组并按游乐设施进行总结。
date rides
0 2019-01-01 247279
1 2019-01-02 585996
2 2019-01-03 660631
3 2019-01-04 662011
4 2019-01-05 440848
.. ... ...
451 2020-03-27 218499
452 2020-03-28 143305
453 2020-03-29 110833
454 2020-03-30 207743
455 2020-03-31 199623
[456 rows x 2 columns]
Run Code Online (Sandbox Code Playgroud)
我的date专栏在datetime64[ns].
date datetime64[ns]
rides int64
dtype: object
Run Code Online (Sandbox Code Playgroud)
现在我想创建另一个数据框,按月份和年份分组(我有 2019 年和 2020 年的数据)并按乘车次数进行汇总。
理想输出:
Year Month Rides
2019 January 2000000
2020 March 1000000
Run Code Online (Sandbox Code Playgroud) 假设我有一个 pydantic 模型
class EventEditRequest(BaseModel):
uid: UUID
name: str
start_dt: datetime
end_dt: datetime
Run Code Online (Sandbox Code Playgroud)
我发送带有正文的请求b'{"uid":"a38a7543-20ca-4a50-ab4e-e6a3ae379d3c","name":"test event2222","start_dt":1600414328,"end_dt":1600450327}'
所以 和start_dt都是end_dtunix时间戳。但在端点中,它们变成带有时区的日期时间。
@app.put('...')
def edit_event(event_data: EventEditRequest):
event_data.start_dt.tzinfo is not None # True
Run Code Online (Sandbox Code Playgroud)
我不想在端点函数中手动编辑start_dt和end_dt来摆脱时区。我如何设置我的 pydantic 模型,使其生成没有时区的日期时间?
如何将 a 转换string为模板datetime内的对象jinja2 ?
我想做类似下面的事情,但 strptime 不被识别。
{% set dtime=strptime(passedintime, '%H:%M:%S') %}
Run Code Online (Sandbox Code Playgroud)
我已经jinja2-time.TimeExtension在环境中启用了。我修改模板之外的字符串的能力非常有限。我基本上只能访问模板本身。
所以我有 -
timezone = pytz.timezone('Asia/Kolkata')
one_time_stamp = '2017-06-01 05:30:00'
zoned_time_stamp = datetime.datetime.strptime(one_time_stamp, '%Y-%m-%d %H:%M:%S')
#This outputs 2017-06-01 05:30:00 which is fine.
print(zoned_time_stamp)
#notice timezone added
non_iso_zoned_ts = zoned_time_stamp.replace(microsecond=0, tzinfo=timezone)
# This outputs 2017-06-01 05:30:00 which is fine.
print(zoned_time_stamp)
iso_date = non_iso_zoned_ts.isoformat()
#This outputs 2017-06-01T05:30:00+05:53 which is incorrect. Ideally it should be 2017-06-01T05:30:00+05:30
print(iso_date)
Run Code Online (Sandbox Code Playgroud)
现在我想知道为什么 isoformat 添加了 05:53 的偏移量,而亚洲/加尔各答时区是 +05:30。参考 - https://www.zeitverschiebung.net/en/timezone/asia--kolkata
我正在尝试使用以下函数获取两个日期之间的天数
df['date'] = pd.to_datetime(df.date)
# Creating a function that returns the number of days
def calculate_days(date):
today = pd.Timestamp('today')
return today - date
# Apply the function to the column date
df['days'] = df['date'].apply(lambda x: calculate_days(x))
Run Code Online (Sandbox Code Playgroud)
结果看起来像这样
153 天 10:16:46.294037
但我想让它说153。我该如何处理这个问题?
python-datetime ×10
python ×9
datetime ×4
pandas ×3
python-3.x ×2
fastapi ×1
jinja2 ×1
pydantic ×1
pytz ×1