I see frequent pandas
examples on SO using time series that have spaces within the timestamps:
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
Or this where the times aren't part of the index:
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
Is there a good way to copy these (or similar) data back into Python to work with? I have found posts like this and this which were lifesavers for getting many examples out of SO, yet I usually fail to find a copy/paste approach (using pd.read_clipboard()
or pd.read_table()
) that works for such data. This often then discourages me from attempting an answer1.
The examples above were created this way:
#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. And for the record, I reckon the onus should be on posters to post their data in a more copy-able format, else not get answered. For time series information, rather than pasting the string representation of the DataFrame I always try to post constructing code (using pd.date_range()
or w/e). And I imagine using something like df.to_dict()
would be better if there are specific (irregular interval) dates that need to be copied for the example.