我正在寻找使用python从s3读取多个分区目录数据的方法.
data_folder/serial_number = 1/cur_date = 20-12-2012/abcdsd0324324.snappy.parquet data_folder/serial_number = 2/cur_date = 27-12-2012/asdsdfsd0324324.snappy.parquet
pyarrow的ParquetDataset模块具有从分区读取的能力.所以我尝试了以下代码:
>>> import pandas as pd
>>> import pyarrow.parquet as pq
>>> import s3fs
>>> a = "s3://my_bucker/path/to/data_folder/"
>>> dataset = pq.ParquetDataset(a)
Run Code Online (Sandbox Code Playgroud)
它引发了以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/my_username/anaconda3/lib/python3.6/site-packages/pyarrow/parquet.py", line 502, in __init__
self.metadata_path) = _make_manifest(path_or_paths, self.fs)
File "/home/my_username/anaconda3/lib/python3.6/site-packages/pyarrow/parquet.py", line 601, in _make_manifest
.format(path))
OSError: Passed non-file path: s3://my_bucker/path/to/data_folder/
Run Code Online (Sandbox Code Playgroud)
根据pyarrow的文档,我尝试使用s3fs作为文件系统,即:
>>> dataset = pq.ParquetDataset(a,filesystem=s3fs)
Run Code Online (Sandbox Code Playgroud)
这会引发以下错误:
Traceback (most recent call last):
File "<stdin>", …Run Code Online (Sandbox Code Playgroud) 我有
import arrow
s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'
Run Code Online (Sandbox Code Playgroud)
如何用Arrow解析这个问题,以便获得带有时区的Arrow对象tz?以下默认为UTC时间.
In [30]: arrow.get(s, 'YYYY/M/D HH:mm:ss')
Out[30]: <Arrow [2015-12-01T19:00:00+00:00]>
Run Code Online (Sandbox Code Playgroud)
我知道该.to功能,但转换时区,但不允许我更改为时区.
我如何让Arrow返回两个时间戳之间的小时差异?
这就是我所拥有的:
difference = arrow.now() - arrow.get(p.create_time())
print(difference.hour)
Run Code Online (Sandbox Code Playgroud)
p.create_time() 是当前正在运行的进程的创建时间的时间戳.
返回:
AttributeError: 'datetime.timedelta' object has no attribute 'hour'
Run Code Online (Sandbox Code Playgroud)
编辑:我不想要所有三种格式的总时间,我希望它作为余数,例如."3天,4小时,36分钟"不是"3天,72小时,4596分钟"
我想使用Arrow类型作为FastAPI响应,因为我已经在SQLAlchemy模型中使用它(感谢sqlalchemy_utils)。
我准备了一个小型的独立示例,其中包含一个最小的 FastAPI 应用程序。我希望这个应用程序product1从数据库返回数据。
不幸的是,下面的代码给出了异常:
Exception has occurred: FastAPIError
Invalid args for response field! Hint: check that <class 'arrow.arrow.Arrow'> is a valid pydantic field type
Run Code Online (Sandbox Code Playgroud)
Exception has occurred: FastAPIError
Invalid args for response field! Hint: check that <class 'arrow.arrow.Arrow'> is a valid pydantic field type
Run Code Online (Sandbox Code Playgroud)
要求.txt:
sqlalchemy==1.4.23
sqlalchemy_utils==0.37.8
arrow==1.1.1
fastapi==0.68.1
uvicorn==0.15.0
Run Code Online (Sandbox Code Playgroud)
这个错误已经在那些 FastAPI 问题中讨论过:
一种可能的解决方法是添加此代码(源代码):
import sqlalchemy
import uvicorn
from arrow import Arrow
from fastapi import …Run Code Online (Sandbox Code Playgroud) 我是 Python 的新手,我一生都无法在网上找到我的具体答案。我需要将时间戳格式化为这种确切格式以包含“T”、“Z”,并且没有像这样的 yyyy-mm-ddThh:mm:ssZ 即 2019-03-06T11:22:00Z 的子或毫秒。有很多关于解析这种格式的东西,但没有关于这种格式的内容。我几乎让它工作的唯一方法涉及我不需要的亚秒。我试过使用箭头并阅读他们的文档,但无法获得任何工作。任何帮助,将不胜感激。
我正在使用箭头模块来处理datetimePython 中的对象。如果我得到这样的当前时间:
now = arrow.now()
Run Code Online (Sandbox Code Playgroud)
...我如何将它增加一天?
我想编写一个函数,该函数接受一个字符串并返回True一个有效的ISO-8601日期时间(精确到微秒,包括时区偏移量),False否则返回。
我发现其他 问题提供了不同的解析日期时间字符串的方式,但是我只想返回TrueISO-8601格式。除非对不匹配ISO-8601的格式抛出错误,否则解析不会对我有帮助。
(我在代码的其他地方使用了漂亮的箭头库。arrow欢迎使用该解决方案。)
编辑:似乎在通用Python日期时间包中不存在“此字符串是否为有效的ISO 8601日期时间”的常规解决方案。
因此,为了使这个问题更窄,更具体和可回答,我将选择一种格式字符串,该格式字符串将验证以下格式的日期时间字符串:
'2016-12-13T21:20:37.593194+00:00'
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用:
format_string = '%Y-%m-%dT%H:%M:%S.%f%z'
datetime.datetime.strptime(my_timestamp, format_string)
Run Code Online (Sandbox Code Playgroud)
这给出:
ValueError: time data '2016-12-13T21:20:37.593194+00:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
Run Code Online (Sandbox Code Playgroud)
问题似乎在于UTC偏移(+00:00)中的冒号。如果我使用不带冒号的偏移量(例如'2016-12-13T21:20:37.593194+0000'),则可以按预期正确解析。显然,这是因为datetime的%z令牌不尊重带有冒号的UTC偏移量格式,只有不包含冒号的格式,即使这两个规范均有效。
我可以从箭头获取当前日期时间,如下所示:
arrow.utcnow().date()
Run Code Online (Sandbox Code Playgroud)
或者
arrow.get('2017-02-01').date()
Run Code Online (Sandbox Code Playgroud)
如何获取前一天的日期时间?这不起作用:
arrow.utcnow().date() - 1
Run Code Online (Sandbox Code Playgroud)
或者
arrow.get('2017-02-01').date() - 1
Run Code Online (Sandbox Code Playgroud) arrow-python ×8
python ×7
datetime ×6
fastapi ×1
fastparquet ×1
format ×1
iso8601 ×1
openapi ×1
parquet ×1
pydantic ×1
timezone ×1
validation ×1