我有以下数据帧对象
total
scanned_date
2021-11-01 0
2021-11-02 0
2021-11-03 0
2021-11-04 0
2021-11-05 0其中scanned_date是Timestamp对象。我想将数据转换为一个元组列表,如
[
(2021-11-01, 0),
(2021-11-02, 0),
(2021-11-03, 0),
...
]但当使用时
list(df.to_records())它正在添加时区,而我只想要日期字符串。
[('2021-11-01T00:00:00.000000000', 0), ('2021-11-02T00:00:00.000000000', 0), ('2021-11-03T00:00:00.000000000', 0)]如何从T00:00:00.00000000 to_records() 输出中删除时区字符串?
发布于 2021-11-19 15:55:04
尝试转换strftime
df.index = df.index.strftime('%Y-%m-%d')
list(df.to_records())
Out[212]:
[('2021-11-01', 0),
('2021-11-02', 0),
('2021-11-03', 0),
('2021-11-04', 0),
('2021-11-05', 0)]发布于 2021-11-19 16:34:05
我试着用numpy做日期转换,但选择了熊猫。在numpy中使用64位整数。我使用map函数和lambda将dataframe记录转换为日期和值元组。
txt="""scanned_date,total
2021-11-01,0
2021-11-02,0
2021-11-03,0
2021-11-04,0
2021-11-05,0
"""
#https://www.py4u.net/discuss/17020
df = pd.read_csv(io.StringIO(txt),sep=',',parse_dates=['scanned_date'])
print(list(map(lambda tuple_obj:
(
pd.to_datetime(tuple_obj[1],'%M/%d/%Y')
#str(tuple_obj[1].astype("datetime64[M]").astype(int)% 12 + 1)
# + "-" + str(tuple_obj[1].astype(object).day)
# + "-" + str(tuple_obj[1].astype("datetime64[Y]"))
,
tuple_obj[2]),
df.to_records())))产出:
[(Timestamp('2021-11-01 00:00:00'), 0), (Timestamp('2021-11-02 00:00:00'), 0), (Timestamp('2021-11-03 00:00:00'), 0), (Timestamp('2021-11-04 00:00:00'), 0), (Timestamp('2021-11-05 00:00:00'), 0)]https://stackoverflow.com/questions/70037541
复制相似问题