我经常看到pandas
使用时间戳中包含空格的时间序列的 SO 示例:
A
2020-01-01 09:20:00 0
2020-01-01 09:21:00 1
2020-01-01 09:22:00 2
2020-01-01 09:23:00 3
2020-01-01 09:24:00 4
或者这是时间不是索引的一部分: dates values cat
0 2020-01-01 09:20:00 0.758513 a
1 2020-01-01 09:21:00 0.337325 b
2 2020-01-01 09:22:00 0.618372 b
3 2020-01-01 09:23:00 0.878714 b
4 2020-01-01 09:24:00 0.311069 b
有没有一种好方法可以将这些(或类似的)数据复制回 Python 以进行处理? 我找到了类似 this 的帖子和 this这是从 SO 中获取许多示例的救星,但我通常找不到适用于此类数据的复制/粘贴方法(使用 pd.read_clipboard()
或 pd.read_table()
)。这通常会阻止我尝试回答1。上面的例子是这样创建的:
#one
import pandas as pd
import numpy
dr = pd.date_range('01-01-2020 9:20', '01-01-2020 9:24', freq='1T')
df1 = pd.DataFrame(index=dr, data=range(len(dr)), columns=['A'])
#two
df2 = pd.DataFrame({'dates':dr,
'values':np.random.rand(len(dr)),
'cat':np.random.choice(['a','b'],len(dr))})
1. 为了记录,我认为海报应该以更易于复制的格式发布他们的数据,否则不会得到答复。对于时间序列信息,我总是尝试发布 ,而不是粘贴 DataFrame 的字符串表示。构建代码 (使用
pd.date_range()
或 w/e)。我想象使用类似 df.to_dict()
的东西如果有需要为示例复制的特定(不规则间隔)日期会更好。
最佳答案
我通常复制整个字符串然后解析它。它并不完美,您通常必须同时编辑字符串和数据框以使其可用。这是一个例子。此解决方案已在此 answer 中提供.我只添加了有关解析日期/时间的内容。
import pandas as pd
from io import StringIO
from dateutil.parser import parse
# I added two more column names `date` and `time`.
# An advantage of having the string in your python code is that
# you can edit it in your text editor/jupyter notebook quickly and directly.
s = """date time A
2020-01-01 09:20:00 0
2020-01-01 09:21:00 1
2020-01-01 09:22:00 2
2020-01-01 09:23:00 3
2020-01-01 09:24:00 4"""
# Parse using whitespace separator. This will still not be perfect as we can
# see below.
df = pd.read_csv(StringIO(s), sep="\s+", index_col=False)
df
# date time A
# 0 2020-01-01 09:20:00 0
# 1 2020-01-01 09:21:00 1
# 2 2020-01-01 09:22:00 2
# 3 2020-01-01 09:23:00 3
# 4 2020-01-01 09:24:00 4
# Combine date and time column together and drop the individual columns.
df['datetime'] = df['date'] + " " + df['time']
df = df.drop(['date', 'time'], axis=1)
# Use a somewhat universal parser in dateutil.parser.parse to parse the
# dates into proper dateime object.
df['datetime'] = df['datetime'].apply(parse)
df
# A datetime
# 0 0 2020-01-01 09:20:00
# 1 1 2020-01-01 09:21:00
# 2 2 2020-01-01 09:22:00
# 3 3 2020-01-01 09:23:00
# 4 4 2020-01-01 09:24:00
df.index
# RangeIndex(start=0, stop=5, step=1)
df.dtypes
# A int64
# datetime datetime64[ns]
# dtype: object
df.columns
# Index(['A', 'datetime'], dtype='object')
在 StackOverflow 上提供格式化和可解析数据帧的一种方法是输出 csv 格式的字符串。# Continued from above
print(df.to_csv(index=False))
# A,datetime
# 0,2020-01-01 09:20:00
# 1,2020-01-01 09:21:00
# 2,2020-01-01 09:22:00
# 3,2020-01-01 09:23:00
# 4,2020-01-01 09:24:00
# We can indeed parse nicely from the csv-formatted string
s_redux = df.to_csv(index=False)
pd.read_csv(StringIO(s_redux))
# A datetime
# 0 0 2020-01-01 09:20:00
# 1 1 2020-01-01 09:21:00
# 2 2 2020-01-01 09:22:00
# 3 3 2020-01-01 09:23:00
# 4 4 2020-01-01 09:24:00
这是解析第二个示例数据帧的一次尝试。和以前一样,我们确实需要对数据框进行一些“编辑”以使其可用。import pandas as pd
from io import StringIO
from dateutil.parser import parse
s=""" dates values cat
0 2020-01-01 09:20:00 0.758513 a
1 2020-01-01 09:21:00 0.337325 b
2 2020-01-01 09:22:00 0.618372 b
3 2020-01-01 09:23:00 0.878714 b
4 2020-01-01 09:24:00 0.311069 b"""
df = pd.read_csv(StringIO(s), sep="\s+").reset_index()
df
# level_0 level_1 dates values cat
# 0 0 2020-01-01 09:20:00 0.758513 a
# 1 1 2020-01-01 09:21:00 0.337325 b
# 2 2 2020-01-01 09:22:00 0.618372 b
# 3 3 2020-01-01 09:23:00 0.878714 b
# 4 4 2020-01-01 09:24:00 0.311069 b
df['dates'] = df['level_1'] + " " + df['dates']
df = df.drop(['level_0', 'level_1'], axis=1)
df['dates'] = df['dates'].apply(parse)
df
# dates values cat
# 0 2020-01-01 09:20:00 0.758513 a
# 1 2020-01-01 09:21:00 0.337325 b
# 2 2020-01-01 09:22:00 0.618372 b
# 3 2020-01-01 09:23:00 0.878714 b
# 4 2020-01-01 09:24:00 0.311069 b
df.dtypes
# dates datetime64[ns]
# values float64
# cat object
# dtype: object
关于python - 如何将带有日期时间的 DataFrames 从 Stack Overflow 复制到 Python 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62744165/