Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

pandas - Comparing data across dates at different times

I have a sequence of data in datetime dataFrame and need to compare the data from one date and time to the prior day's another time. In the example below, I would, for example need to calculate percentage change from 2016-11-09 09:30:00 to 2016-11-10 21:30:00 (essentially from t @ 9:30 to next_date in the table @ 21:30.) Unfortunately a simple lag function might not work, since the pattern of the records are unpredictable and there might not be the same number of records between the desired rows.

dates = pd.date_range('2016-11-09 09:30:00',periods=10, freq='12H')
df =pd.DataFrame(np.random.randn(10,4)*100,index=dates,columns=list('ABCD'))

                              A|           B|           C|           D
-----------------------------------------------------------------------
2016-11-09 09:30:00|   74.409062|    3.635309|   17.603051|    6.743699

2016-11-09 21:30:00|   25.707464|  133.592600| -176.460798|  236.354740

2016-11-10 09:30:00|  -13.035709|  -82.974810|  106.204290|  -31.382023

2016-11-10 21:30:00| -120.712954|   -2.636682|   16.839875|  -12.177463

2016-11-11 09:30:00| -195.382169| -102.214945|   84.151532| -130.732630

....

Is there an easy way to do this comparison or would I need to run a for loop?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the best approach might be to split the two set of records at the two different times into two DataFrames, align the dates, get rid of the times and compare them across the two DataFrames.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...