0

我正在尝试使用S3 Select读取镶木地板文件,但是当数据包含缺失值时遇到问题 - 从 S3 select 返回的结果会跳过所有缺失值,从而无法解析输出。python和boto3的可重现示例:

import pandas as pd
import numpy as np
import boto3

session = boto3.session.Session()
s3 = session.client('s3')

df = pd.DataFrame({'A': [1.0, 2.0, 3.0], 'B': [5, np.nan, 7]})
df['C'] = np.nan
print(df)

# Prints:
#        A    B   C
#   0  1.0  5.0 NaN
#   1  2.0  NaN NaN
#   2  3.0  7.0 NaN

bucket = 'your-test-bucket'
key = 'temp/s3_select/df.parquet'
df.to_parquet(f's3://{bucket}/{key}')

r = s3.select_object_content(
    Bucket=bucket,
    Key=key,
    ExpressionType='SQL',
    Expression='select s.A, s.B, s.C from s3object s',
    InputSerialization = {'Parquet': {}},
    OutputSerialization = {'CSV': {}},
)

records = []
for event in r['Payload']:
    if 'Records' in event:
        records.append(event['Records']['Payload'].decode('utf-8'))
print(records[0])

# Prints:
#    1.0,5.0
#    2.0
#    3.0,7.0

即,所有缺失值都被简单地跳过。

有没有办法获得适当编码的缺失值的结果?

4

1 回答 1

0

这确实是 AWS S3 中的一个错误,截至 2019 年 5 月 9 日,已修复。上面的代码现在产生

1.0,5.0,
2.0,,
3.0,7.0,
于 2019-05-20T22:03:36.407 回答