Tom*_*Tom 6 python clipboard copy-paste time-series pandas
我经常看到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
Run Code Online (Sandbox Code Playgroud)
或者这是时间不是索引的一部分:
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
Run Code Online (Sandbox Code Playgroud)
有没有一种好方法可以将这些(或类似的)数据复制回 Python 以进行处理? 我发现像帖子这个和这个这是为获得很多例子了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))})
Run Code Online (Sandbox Code Playgroud)
1. 为了记录,我认为海报应该有责任以更易于复制的格式发布他们的数据,否则不会得到答复。对于时间序列信息,我总是尝试发布构造代码(使用pd.date_range()
或 w/e),而不是粘贴 DataFrame 的字符串表示。我想df.to_dict()
如果有需要为示例复制的特定(不规则间隔)日期,使用类似的东西会更好。
我通常复制整个字符串然后解析它。它并不完美,您通常必须编辑字符串和数据框才能使其可用。这是一个例子。这个答案已经提供了这个解决方案。我只添加了有关解析日期/时间的部分。
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')
Run Code Online (Sandbox Code Playgroud)
在 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
Run Code Online (Sandbox Code Playgroud)
这是解析第二个示例数据帧的一次尝试。和以前一样,我们确实需要对数据框进行一些“编辑”以使其可用。
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
Run Code Online (Sandbox Code Playgroud)