How to get the average of a time frame in pandas - python

I have a large CSV file as below:
dd hh v.amm v.alc v.no2 v.cmo aqi
t
2018-11-03 00:00:00 3 0 0.390 0.490 1.280 1.760 2.560
2018-11-03 00:01:00 3 0 0.390 0.490 1.280 1.760 2.560
2018-11-03 00:02:00 3 0 0.380 0.460 1.300 1.610 2.500
2018-11-03 00:03:00 3 0 0.380 0.450 1.310 1.600 2.490
...
2018-11-28 23:56:00 28 23 0.670 0.560 1.100 1.870 2.940
2018-11-28 23:57:00 28 23 0.660 0.570 1.100 1.990 2.950
2018-11-28 23:58:00 28 23 0.660 0.570 1.100 1.990 2.950
2018-11-28 23:59:00 28 23 0.650 0.530 1.130 1.880 2.870
[37440 rows x 7 columns]
I'd like to take the average of 60 minutes to obtain hourly data. The final data would look something like this:
dd hh v.amm v.alc v.no2 v.cmo aqi
t
2018-11-03 00:00:00 3 0 0.390 0.490 1.280 1.760 2.560
2018-11-03 01:00:00 3 1 0.390 0.490 1.280 1.760 2.560
2018-11-03 02:00:00 3 2 0.380 0.460 1.300 1.610 2.500
2018-11-03 03:00:00 3 3 0.380 0.450 1.310 1.600 2.490
I tried
print (df['v.amm'].resample('60Min').mean())
t
2018-11-03 00:00:00 0.357
2018-11-03 01:00:00 0.354
2018-11-03 02:00:00 0.369
2018-11-03 03:00:00 0.384
but I don't think it's efficient as it only prints one specific column at a time, without heading as well.

Related

Unable to set index as x axis

I have the following DF :
A Index B Index C Index D Index
PX_LAST PX_LAST PX_LAST PX_LAST
2021-12-31 1.5101 0.195 -2.101 -0.509
2022-01-03 1.628 0.244 -2.032 -0.468
2022-01-04 1.6473 0.233 -2.074 -0.511
2022-01-05 1.7052 0.229 -2.045 -0.468
2022-01-06 1.7211 0.261 -1.965 -0.37
2022-01-07 1.762 0.285 -1.97 -0.338
2022-01-10 1.7603 0.284 -1.964 -0.361
2022-01-11 1.7357 0.347 -1.961 -0.348
2022-01-12 1.7428 0.321 -1.995 -0.384
2022-01-13 1.7041 0.288 -1.993 -0.394
2022-01-14 1.7841 0.332 -1.959 -0.352
2022-01-17 1.7841 0.355 -1.948 -0.339
2022-01-18 1.8735 0.368 -1.941 -0.311
2022-01-19 1.8646 0.38 -1.924 -0.283
2022-01-20 1.804 0.363 -1.918 -0.306
2022-01-21 1.7581 0.332 -1.925 -0.291
2022-01-24 1.7706 0.305 -1.959 -0.28
2022-01-25 1.7689 0.331 -1.954 -0.294
2022-01-26 1.8637 0.336 -1.951 -0.265
2022-01-27 1.7994 0.344 -1.943 -0.33
2022-01-28 1.7694 0.367 -1.95 -0.365
2022-01-31 1.7767 0.424 -1.969 -0.402
When I try to plot it doing :
df.plot(x=df.index,y=["A Index","B Index","X Index","D Index"])
it does throw the following issue
KeyError: "Index([2021-12-31, 2022-01-03, 2022-01-04, 2022-01-05, 2022-01-06, 2022-01-07,\n 2022-01-10, 2022-01-11, 2022-01-12, 2022-01-13,\n ...\n dtype='object', length=135) not in index"
What are these '\n'? How can I plot this DF ?
Many Thanks
The \n are likely just a representation of the newline characters in the error message.
Plotting by index is the default behavior if no x is specified. df.plot(y=["A Index", "B Index"]) gives

Is there any way to use Groupby and Rollong together?

I have the following dataframe with daily data:
day value
2017-08-04 0.832
2017-08-05 0.892
2017-08-06 0.847
2017-08-07 0.808
2017-08-08 0.922
2017-08-09 0.894
2017-08-10 2.332
2017-08-11 0.886
2017-08-12 0.973
2017-08-13 0.980
... ...
2022-03-21 0.821
2022-03-22 1.121
2022-03-23 1.064
2022-03-24 1.058
2022-03-25 0.891
2022-03-26 1.010
2022-03-27 1.023
2022-03-28 1.393
2022-03-29 2.013
2022-03-30 3.872
[1700 rows x 1 columns]
I need to generate pooled averages using moving windows. I explain it group by group:
The first group must contain the data from 2017-08-04 to 2017-08-08, but also the data from 2018-08-04 to 2018-08-08, and so on until the last year. As shown below:
2017-08-04 0.832
2017-08-05 0.892
2017-08-06 0.847
2017-08-07 0.808
2017-08-08 0.922
---------- -----
2018-08-04 2.125
2018-08-05 2.200
2018-08-06 2.339
2018-08-07 2.035
2018-08-08 1.953
... ...
2020-08-04 0.965
2020-08-05 0.941
2020-08-06 0.917
2020-08-07 0.922
2020-08-08 0.909
---------- -----
2021-08-04 1.348
2021-08-05 1.302
2021-08-06 1.272
2021-08-07 1.258
2021-08-08 1.281
The second group must run one day the temporary window. That is, data from 2017-08-05 to 2017-08-09, from 2018-08-05 to 2018-08-09, and so on until the last year. As shown below:
2017-08-05 0.892
2017-08-06 0.847
2017-08-07 0.808
2017-08-08 0.922
2017-08-09 1.823
---------- -----
2018-08-05 2.200
2018-08-06 2.339
2018-08-07 2.035
2018-08-08 1.953
2018-08-09 2.009
... ...
2020-08-05 0.941
2020-08-06 0.917
2020-08-07 0.922
2020-08-08 0.909
2020-08-09 1.934
---------- -----
2021-08-05 1.302
2021-08-06 1.272
2021-08-07 1.258
2021-08-08 1.281
2021-08-09 2.348
And the following groups must follow the same dynamic. Finally, I need to form a DataFrame, where the indices are the central date of each window (the length of the DataFrame will be 365 days of the year) and the values ​​are the average of each of the groups mentioned above.
I have been trying Groupby and Rolling at the same time. But any solution based on other pandas methods is completely valid.

Pandas: cumulative sum every n rows

I have a dataframe with a column "date" of type dtype M8[ns] and another "expected_response". Then, there is a column "cumulative_expected" which does the cumulative sum of the expected_response among the rows with the same date. The dataframe has a row for each second of the month. Like below:
date Expected_response cumulative_expected
0 2018-03-01 0.270 0.270
1 2018-03-01 0.260 0.530
2 2018-03-01 0.240 0.770
3 2018-03-01 0.224 0.994
4 2018-03-01 0.204 1.198
5 2018-03-01 0.194 1.392
6 2018-03-01 0.190 1.582
... ... ... ...
2678395 2018-03-31 0.164 -7533.464
2678396 2018-03-31 0.164 -7533.300
2678397 2018-03-31 0.160 -7533.140
2678398 2018-03-31 0.154 -7532.986
2678399 2018-03-31 0.150 -7532.836
as you can see there is an error: the cumulative sum does not recognise the Change of the date and the cumulative sum does not restart each time the date changes.
The code is:
df['cumulative_expected']=df.groupby(df['date']!=df['date'])['Expected_response'].cumsum()
Maybe an Option could be to create a Counter that increases by 1 each 86400 rows (seconds in a day) and then groupby the Counter. But I don't know how to do it.
Is there any other solution?
thank you in advance
There is default index, so you can use floor division:
df['cumulative_expected'] = df['Expected_response'].groupby(df.index // 86400).cumsum()
Generally solution is create np.arange with floor division:
arr = np.arange(len(df)) // 86400
df['cumulative_expected'] = df['Expected_response'].groupby(arr).cumsum()
Your solution should be changed with comparing shifted values with cumsum:
s = (df['date']!=df['date'].shift()).cumsum()
df['cumulative_expected'] = df['Expected_response'].groupby(s).cumsum()
Test with changed sample data:
print (df)
date Expected_response
0 2018-03-01 0.270
1 2018-03-01 0.260
2 2018-03-02 0.240
3 2018-03-02 0.224
4 2018-03-02 0.204
5 2018-03-01 0.194
6 2018-03-01 0.190
s = (df['date']!=df['date'].shift()).cumsum()
print (s)
0 1
1 1
2 2
3 2
4 2
5 3
6 3
Name: date, dtype: int32
df['cumulative_expected'] = df['Expected_response'].groupby(s).cumsum()
print (df)
date Expected_response cumulative_expected
0 2018-03-01 0.270 0.270
1 2018-03-01 0.260 0.530
2 2018-03-02 0.240 0.240
3 2018-03-02 0.224 0.464
4 2018-03-02 0.204 0.668
5 2018-03-01 0.194 0.194
6 2018-03-01 0.190 0.384
You can take the first difference of the date using diff to see were the changes occur, and use this as a reference to take the cumulative sum.
Here I use a slightly modified df to see how works:
print(df)
date Expected_response
0 2018-03-01 0.270
1 2018-03-01 0.260
2 2018-03-01 0.240
3 2018-03-01 0.224
4 2018-03-02 0.204
5 2018-03-02 0.194
6 2018-03-02 0.190
df['change'] = df.date.diff().abs().fillna(0).cumsum()
print(df)
date Expected_response change
0 2018-03-01 0.270 0 days
1 2018-03-01 0.260 0 days
2 2018-03-01 0.240 0 days
3 2018-03-01 0.224 0 days
4 2018-03-02 0.204 1 days
5 2018-03-02 0.194 1 days
6 2018-03-02 0.190 1 days
df['cumulative_expected'] = df.groupby('change').cumsum()
print(df.drop(['change'], axis = 1))
date Expected_response cumulative_expected
0 2018-03-01 0.270 0.270
1 2018-03-01 0.260 0.530
2 2018-03-01 0.240 0.770
3 2018-03-01 0.224 0.994
4 2018-03-02 0.204 0.204
5 2018-03-02 0.194 0.398
6 2018-03-02 0.190 0.588

Creating a temporal range time-series spiral plot

Similarly to this question, I'm interested in creating time series spirals. The solution doesn't necessarily have to be implemented in R or using ggplot, but it seems the majority of solutions have been implemented in R with ggplot, with a handful in Python and one in d3. My attempts so far have all used R. Unlike this question, I'm interested in displaying specific ranges of data without quantizing/binning the data. That is, I'd like to display a spiral timeline showing when particular events start and stop, where theta-min and theta-max of every event represent specific points in time.
Consider this travel data:
trip_start trip_stop dist
2017-04-01 17:42:00 2017-04-01 18:34:00 1.95
2017-04-01 18:42:00 2017-04-01 19:05:00 6.54
2017-04-02 01:09:00 2017-04-02 01:12:00 1.07
2017-04-02 01:22:00 2017-04-02 01:27:00 1.03
2017-04-02 08:17:00 2017-04-02 08:23:00 1.98
2017-04-02 11:23:00 2017-04-02 11:30:00 1.98
2017-04-02 15:44:00 2017-04-02 15:56:00 4.15
2017-04-02 16:29:00 2017-04-02 16:45:00 4.08
2017-04-03 10:24:00 2017-04-03 10:55:00 19.76
2017-04-03 14:01:00 2017-04-03 14:18:00 8.21
2017-04-03 14:25:00 2017-04-03 14:31:00 1.49
2017-04-03 14:45:00 2017-04-03 14:50:00 1.59
2017-04-03 15:44:00 2017-04-03 16:10:00 4.44
2017-04-03 16:14:00 2017-04-03 16:37:00 9.96
2017-04-03 16:40:00 2017-04-03 16:45:00 0.7
2017-04-03 17:15:00 2017-04-03 17:46:00 16.92
2017-04-03 17:56:00 2017-04-03 18:19:00 5.23
2017-04-03 18:42:00 2017-04-03 18:45:00 0.49
2017-04-03 19:02:00 2017-04-03 19:04:00 0.48
2017-04-04 07:24:00 2017-04-04 07:27:00 0.66
2017-04-04 07:30:00 2017-04-04 08:04:00 13.55
2017-04-04 08:32:00 2017-04-04 09:25:00 25.09
2017-04-04 13:32:00 2017-04-04 13:40:00 3.06
2017-04-04 13:52:00 2017-04-04 13:57:00 1.3
2017-04-04 14:55:00 2017-04-04 15:01:00 2.47
2017-04-04 18:40:00 2017-04-04 19:12:00 22.71
2017-04-04 22:16:00 2017-04-04 23:54:00 38.28
2017-04-04 23:59:00 2017-04-05 00:03:00 1.02
2017-04-05 11:04:00 2017-04-05 11:49:00 25.73
2017-04-05 12:05:00 2017-04-05 12:18:00 2.97
2017-04-05 15:19:00 2017-04-05 16:25:00 25.13
2017-04-05 16:38:00 2017-04-05 16:40:00 0.41
2017-04-05 18:58:00 2017-04-05 19:02:00 1.25
2017-04-05 19:13:00 2017-04-05 19:18:00 1.09
2017-04-05 19:25:00 2017-04-05 19:48:00 6.63
2017-04-06 10:01:00 2017-04-06 10:44:00 20.81
2017-04-06 13:22:00 2017-04-06 13:33:00 1.63
2017-04-06 20:58:00 2017-04-06 21:25:00 24.85
2017-04-06 21:32:00 2017-04-06 21:56:00 6.06
2017-04-07 10:55:00 2017-04-07 11:37:00 24.53
2017-04-07 17:14:00 2017-04-07 17:48:00 19.66
2017-04-07 17:57:00 2017-04-07 18:07:00 2.12
2017-04-08 20:57:00 2017-04-08 21:06:00 1.06
2017-04-08 21:23:00 2017-04-08 21:36:00 2.97
2017-04-09 08:14:00 2017-04-09 08:19:00 1.99
2017-04-09 11:40:00 2017-04-09 11:50:00 2.24
2017-04-09 11:50:00 2017-04-09 11:57:00 1.64
2017-04-09 16:29:00 2017-04-09 16:34:00 0.53
2017-04-09 16:43:00 2017-04-09 16:45:00 0.5
2017-04-09 17:46:00 2017-04-09 17:48:00 0.44
2017-04-09 17:53:00 2017-04-09 17:56:00 0.4
2017-04-09 21:33:00 2017-04-09 21:56:00 2.48
2017-04-09 21:57:00 2017-04-09 22:14:00 2.92
2017-04-09 22:22:00 2017-04-09 22:25:00 0.9
2017-04-10 10:37:00 2017-04-10 11:22:00 19.27
2017-04-10 16:12:00 2017-04-10 16:59:00 21.31
2017-04-11 11:14:00 2017-04-11 11:18:00 1.24
2017-04-11 11:21:00 2017-04-11 11:48:00 22.95
2017-04-11 18:24:00 2017-04-11 19:05:00 28.64
2017-04-11 19:21:00 2017-04-11 19:34:00 5.37
2017-04-12 11:00:00 2017-04-12 12:08:00 28.91
2017-04-12 14:03:00 2017-04-12 15:20:00 28.56
2017-04-12 20:24:00 2017-04-12 20:29:00 1.17
2017-04-12 20:32:00 2017-04-12 21:09:00 30.89
2017-04-13 01:37:00 2017-04-13 02:09:00 32.3
2017-04-13 08:08:00 2017-04-13 08:39:00 19.39
2017-04-13 10:53:00 2017-04-13 11:23:00 24.59
2017-04-13 18:56:00 2017-04-13 19:22:00 22.74
2017-04-14 01:06:00 2017-04-14 01:37:00 31.36
2017-04-14 01:48:00 2017-04-14 01:51:00 1.03
2017-04-14 12:08:00 2017-04-14 12:22:00 1.94
2017-04-14 12:29:00 2017-04-14 13:01:00 19.07
2017-04-14 16:17:00 2017-04-14 17:03:00 19.74
2017-04-14 17:05:00 2017-04-14 17:32:00 3.99
2017-04-14 21:57:00 2017-04-14 22:02:00 1.98
2017-04-15 01:46:00 2017-04-15 01:49:00 1.07
2017-04-15 01:56:00 2017-04-15 01:58:00 1.03
2017-04-15 07:13:00 2017-04-15 07:15:00 0.45
2017-04-15 07:19:00 2017-04-15 07:21:00 0.41
2017-04-15 15:54:00 2017-04-15 16:05:00 1.94
2017-04-15 22:23:00 2017-04-15 22:26:00 0.86
2017-04-15 22:46:00 2017-04-15 22:47:00 0.25
2017-04-15 22:51:00 2017-04-15 22:53:00 0.71
2017-04-16 11:35:00 2017-04-16 11:54:00 11.4
2017-04-16 11:58:00 2017-04-16 12:15:00 10.43
2017-04-17 10:44:00 2017-04-17 10:53:00 3.04
2017-04-17 10:55:00 2017-04-17 11:22:00 18.26
2017-04-17 18:09:00 2017-04-17 18:12:00 0.85
2017-04-17 18:21:00 2017-04-17 19:07:00 37.22
2017-04-18 02:07:00 2017-04-18 02:47:00 32.41
2017-04-18 10:55:00 2017-04-18 10:57:00 0.41
2017-04-18 11:02:00 2017-04-18 11:12:00 2.3
2017-04-18 11:15:00 2017-04-18 11:52:00 24.05
2017-04-18 16:59:00 2017-04-18 17:55:00 22.66
2017-04-19 00:46:00 2017-04-19 01:35:00 39.25
2017-04-19 10:57:00 2017-04-19 11:44:00 24.06
2017-04-19 13:23:00 2017-04-19 14:10:00 25.96
2017-04-19 16:21:00 2017-04-19 17:07:00 18.05
2017-04-19 23:32:00 2017-04-20 00:19:00 39.67
2017-04-20 10:47:00 2017-04-20 11:13:00 24.07
2017-04-20 16:21:00 2017-04-20 16:30:00 0.86
2017-04-20 16:36:00 2017-04-20 16:58:00 0.85
2017-04-20 17:41:00 2017-04-20 17:44:00 0.37
2017-04-20 17:49:00 2017-04-20 18:40:00 19.32
2017-04-20 22:22:00 2017-04-20 22:53:00 29.2
2017-04-20 23:07:00 2017-04-20 23:27:00 10.94
2017-04-21 08:29:00 2017-04-21 08:40:00 1.91
2017-04-21 11:30:00 2017-04-21 11:32:00 0.42
2017-04-21 11:38:00 2017-04-21 11:40:00 0.4
2017-04-21 11:42:00 2017-04-21 12:15:00 19.09
2017-04-21 16:50:00 2017-04-21 18:17:00 40.61
2017-04-21 18:55:00 2017-04-21 19:11:00 1.73
2017-04-21 22:20:00 2017-04-21 22:53:00 28.26
2017-04-21 23:01:00 2017-04-21 23:22:00 11.76
2017-04-22 08:56:00 2017-04-22 08:58:00 0.63
2017-04-22 09:04:00 2017-04-22 09:08:00 0.3
2017-04-22 09:12:00 2017-04-22 09:15:00 0.42
2017-04-22 16:48:00 2017-04-22 16:52:00 0.54
2017-04-22 17:06:00 2017-04-22 17:09:00 0.51
2017-04-22 17:10:00 2017-04-22 17:13:00 1.03
2017-04-22 17:22:00 2017-04-22 17:27:00 1.1
2017-04-23 08:13:00 2017-04-23 08:15:00 0.41
2017-04-23 08:19:00 2017-04-23 08:20:00 0.4
2017-04-23 08:21:00 2017-04-23 08:25:00 1.99
2017-04-23 11:41:00 2017-04-23 11:48:00 2.04
2017-04-23 12:35:00 2017-04-23 12:50:00 7.59
2017-04-23 14:08:00 2017-04-23 14:21:00 7.31
2017-04-23 14:33:00 2017-04-23 15:38:00 37.6
2017-04-24 00:26:00 2017-04-24 01:18:00 39.21
2017-04-24 10:24:00 2017-04-24 10:26:00 0.41
2017-04-24 10:31:00 2017-04-24 10:35:00 1.37
2017-04-24 10:38:00 2017-04-24 10:43:00 1.19
2017-04-24 10:49:00 2017-04-24 11:15:00 19.58
2017-04-24 17:13:00 2017-04-24 18:20:00 37.42
2017-04-24 19:02:00 2017-04-24 19:08:00 1.76
2017-04-24 19:49:00 2017-04-24 19:55:00 1.79
2017-04-24 20:41:00 2017-04-24 21:16:00 32.31
2017-04-25 10:53:00 2017-04-25 11:25:00 24.83
2017-04-25 15:15:00 2017-04-25 15:24:00 3.07
2017-04-25 15:30:00 2017-04-25 15:40:00 3.01
2017-04-25 17:34:00 2017-04-25 18:18:00 24.8
2017-04-26 09:59:00 2017-04-26 10:28:00 24.05
2017-04-26 12:56:00 2017-04-26 13:40:00 29.13
2017-04-26 14:37:00 2017-04-26 15:34:00 21
2017-04-27 08:57:00 2017-04-27 10:21:00 40.56
2017-04-27 16:12:00 2017-04-27 16:44:00 9.89
2017-04-27 17:09:00 2017-04-27 18:01:00 17.51
2017-04-28 05:18:00 2017-04-28 06:06:00 39.28
2017-04-28 12:57:00 2017-04-28 13:52:00 35.82
2017-04-28 16:48:00 2017-04-28 18:14:00 39.1
2017-05-01 11:41:00 2017-05-01 12:20:00 18.74
2017-05-01 18:53:00 2017-05-01 19:34:00 37.15
2017-05-01 23:08:00 2017-05-01 23:09:00 0.06
2017-05-01 23:18:00 2017-05-02 00:11:00 38.61
2017-05-02 11:05:00 2017-05-02 11:42:00 24.07
2017-05-02 17:34:00 2017-05-02 18:53:00 26.42
2017-05-03 12:13:00 2017-05-03 12:25:00 3.96
2017-05-03 12:25:00 2017-05-03 12:56:00 21.15
2017-05-03 13:26:00 2017-05-03 13:44:00 3.32
2017-05-03 13:57:00 2017-05-03 14:08:00 3.49
2017-05-03 18:39:00 2017-05-03 19:08:00 24.85
2017-05-03 19:09:00 2017-05-03 19:13:00 0.99
2017-05-03 19:29:00 2017-05-03 19:32:00 0.84
2017-05-04 10:38:00 2017-05-04 11:06:00 24.05
2017-05-04 13:34:00 2017-05-04 14:10:00 1.73
2017-05-04 17:14:00 2017-05-04 18:23:00 24.68
2017-05-05 20:38:00 2017-05-05 20:52:00 2.24
2017-05-06 11:45:00 2017-05-06 12:30:00 20.19
2017-05-06 14:36:00 2017-05-06 15:35:00 14.49
2017-05-06 15:48:00 2017-05-06 16:17:00 5.25
2017-05-06 17:11:00 2017-05-06 17:13:00 0.43
2017-05-06 17:19:00 2017-05-06 17:21:00 0.43
2017-05-07 08:16:00 2017-05-07 08:22:00 3.27
2017-05-07 12:09:00 2017-05-07 12:16:00 2.01
2017-05-07 17:28:00 2017-05-07 17:50:00 10.36
2017-05-07 17:54:00 2017-05-07 18:01:00 1.19
2017-05-07 18:02:00 2017-05-07 18:35:00 28.31
2017-05-07 21:48:00 2017-05-07 21:52:00 1.46
2017-05-07 22:01:00 2017-05-07 22:05:00 1.37
2017-05-08 00:59:00 2017-05-08 02:19:00 39.23
2017-05-08 11:30:00 2017-05-08 11:58:00 22.55
2017-05-08 18:08:00 2017-05-08 18:30:00 10.47
2017-05-08 18:33:00 2017-05-08 19:09:00 28.44
2017-05-08 22:25:00 2017-05-08 23:09:00 38.65
2017-05-08 23:14:00 2017-05-08 23:17:00 1.04
2017-05-09 11:35:00 2017-05-09 12:19:00 23.99
2017-05-09 17:57:00 2017-05-09 18:59:00 29.38
2017-05-09 20:03:00 2017-05-09 20:13:00 1.9
2017-05-10 10:18:00 2017-05-10 10:54:00 24.06
2017-05-10 15:43:00 2017-05-10 16:46:00 24.71
2017-05-11 12:28:00 2017-05-11 13:07:00 21.75
2017-05-11 18:00:00 2017-05-11 18:31:00 19.3
2017-05-12 08:26:00 2017-05-12 08:55:00 20.46
2017-05-12 13:00:00 2017-05-12 13:34:00 14.6
2017-05-13 08:44:00 2017-05-13 08:46:00 0.38
2017-05-13 08:57:00 2017-05-13 09:01:00 0.33
2017-05-13 14:22:00 2017-05-13 14:41:00 6.86
2017-05-13 15:17:00 2017-05-13 15:35:00 5.2
2017-05-13 18:10:00 2017-05-13 18:21:00 1.91
2017-05-14 11:22:00 2017-05-14 11:26:00 0.9
2017-05-14 11:36:00 2017-05-14 11:38:00 0.39
2017-05-14 14:56:00 2017-05-14 15:59:00 40.07
2017-05-14 16:34:00 2017-05-14 16:41:00 1.49
2017-05-14 16:56:00 2017-05-14 17:04:00 1.45
2017-05-14 19:05:00 2017-05-14 20:06:00 39.21
2017-05-15 11:24:00 2017-05-15 11:33:00 1.91
2017-05-15 11:41:00 2017-05-15 12:13:00 19.84
2017-05-15 17:41:00 2017-05-15 18:11:00 16
2017-05-15 18:15:00 2017-05-15 19:23:00 31.52
2017-05-15 23:41:00 2017-05-16 00:26:00 39.32
2017-05-16 09:49:00 2017-05-16 11:02:00 24.91
2017-05-16 16:08:00 2017-05-16 16:32:00 3.37
2017-05-16 17:11:00 2017-05-16 17:32:00 4.8
2017-05-16 17:42:00 2017-05-16 17:56:00 1.81
2017-05-16 18:13:00 2017-05-16 18:46:00 24.85
2017-05-16 21:07:00 2017-05-16 21:10:00 1.04
2017-05-16 21:26:00 2017-05-16 21:29:00 1.02
2017-07-28 16:10:00 2017-07-28 16:17:00 2.22
2017-07-28 16:17:00 2017-07-28 16:42:00 7.84
2017-08-10 12:00:00 2017-08-10 12:44:00 24.05
2017-08-10 14:56:00 2017-08-10 15:10:00 1.61
2017-08-10 18:51:00 2017-08-10 19:21:00 24.85
2017-08-10 19:46:00 2017-08-10 19:56:00 1.14
2017-08-10 20:08:00 2017-08-10 20:12:00 1.09
2017-08-11 12:44:00 2017-08-11 12:49:00 0.82
2017-08-11 12:59:00 2017-08-11 13:01:00 0.56
2017-08-11 13:18:00 2017-08-11 15:12:00 1.79
2017-08-11 15:14:00 2017-08-11 16:53:00 34.6
2017-08-11 19:27:00 2017-08-11 20:34:00 34.91
2017-08-12 13:52:00 2017-08-12 13:56:00 1.05
2017-08-12 13:59:00 2017-08-12 14:02:00 0.28
2017-08-12 14:10:00 2017-08-12 14:30:00 1.22
2017-08-12 17:15:00 2017-08-12 17:36:00 11.37
2017-08-12 20:49:00 2017-08-12 21:05:00 10.43
2017-08-13 12:16:00 2017-08-13 12:44:00 12.96
2017-08-13 16:03:00 2017-08-13 16:32:00 14.33
2017-08-13 18:19:00 2017-08-13 18:42:00 9.32
2017-08-13 18:52:00 2017-08-13 19:05:00 3.99
2017-08-13 21:42:00 2017-08-13 21:53:00 5.6
2017-08-14 08:50:00 2017-08-14 09:45:00 24.1
2017-08-14 13:22:00 2017-08-14 13:54:00 24.84
2017-08-14 14:02:00 2017-08-14 15:34:00 36.92
2017-08-14 15:58:00 2017-08-14 17:17:00 35.7
2017-08-14 17:35:00 2017-08-14 17:45:00 1.99
2017-08-14 18:07:00 2017-08-14 18:27:00 9.92
2017-08-15 10:15:00 2017-08-15 10:51:00 25
2017-08-15 19:23:00 2017-08-15 19:29:00 0.4
2017-08-15 19:51:00 2017-08-15 20:45:00 24.39
2017-08-15 20:56:00 2017-08-15 21:04:00 2.78
2017-08-15 21:09:00 2017-08-15 21:37:00 19.22
2017-08-16 00:03:00 2017-08-16 00:27:00 15.51
2017-08-16 00:36:00 2017-08-16 00:41:00 1.23
2017-08-16 00:46:00 2017-08-16 01:18:00 11.35
2017-08-16 09:38:00 2017-08-16 09:41:00 1.21
2017-08-16 09:41:00 2017-08-16 09:43:00 0.08
2017-08-16 09:47:00 2017-08-16 10:32:00 22.89
2017-08-16 16:51:00 2017-08-16 17:11:00 3.14
2017-08-16 17:12:00 2017-08-16 17:25:00 2.76
2017-08-16 17:41:00 2017-08-16 18:36:00 24.78
2017-08-17 09:34:00 2017-08-17 10:13:00 24.03
2017-08-17 12:32:00 2017-08-17 13:07:00 24.82
2017-08-17 13:35:00 2017-08-17 13:40:00 0.4
2017-08-17 13:47:00 2017-08-17 15:07:00 36.06
2017-08-17 15:18:00 2017-08-17 15:24:00 0.06
2017-08-17 16:03:00 2017-08-17 18:05:00 35.16
2017-08-18 09:47:00 2017-08-18 10:23:00 24.47
2017-08-18 16:04:00 2017-08-18 16:42:00 1.63
2017-08-18 17:56:00 2017-08-18 18:25:00 10.74
2017-08-18 18:27:00 2017-08-18 18:48:00 1.85
2017-08-19 00:07:00 2017-08-19 00:41:00 18.92
2017-08-19 00:52:00 2017-08-19 00:55:00 0.99
2017-08-19 11:52:00 2017-08-19 12:14:00 7.56
2017-08-19 15:57:00 2017-08-19 16:12:00 4.02
2017-08-19 16:37:00 2017-08-19 16:56:00 5.32
2017-08-19 23:32:00 2017-08-19 23:50:00 7.54
2017-08-19 23:51:00 2017-08-20 00:17:00 9.59
2017-08-20 09:03:00 2017-08-20 09:16:00 5.22
2017-08-20 19:17:00 2017-08-20 19:32:00 4.69
2017-08-21 09:24:00 2017-08-21 09:40:00 2.31
2017-08-21 10:59:00 2017-08-21 11:02:00 0.47
2017-08-21 13:40:00 2017-08-21 15:29:00 36.09
2017-08-21 15:54:00 2017-08-21 16:48:00 2.24
2017-08-21 16:57:00 2017-08-21 18:15:00 32.3
2017-08-22 08:38:00 2017-08-22 09:06:00 0.65
2017-08-22 09:18:00 2017-08-22 09:19:00 0.04
2017-08-22 09:22:00 2017-08-22 10:05:00 23.49
2017-08-22 14:30:00 2017-08-22 15:02:00 1.7
2017-08-22 16:37:00 2017-08-22 17:41:00 24.8
2017-08-23 17:16:00 2017-08-23 18:14:00 24.01
2017-08-23 18:27:00 2017-08-23 18:32:00 1.05
2017-08-23 19:24:00 2017-08-23 20:04:00 18.14
2017-08-23 22:01:00 2017-08-23 22:28:00 16.33
2017-08-23 22:46:00 2017-08-23 22:50:00 1.04
2017-08-24 09:41:00 2017-08-24 09:44:00 0.02
2017-08-24 09:59:00 2017-08-24 10:00:00 0.02
2017-08-24 13:57:00 2017-08-24 15:33:00 42.51
2017-08-24 16:43:00 2017-08-24 17:00:00 0.07
2017-08-24 17:06:00 2017-08-24 17:33:00 10.01
2017-08-24 18:12:00 2017-08-24 19:03:00 27.67
2017-08-25 09:36:00 2017-08-25 09:55:00 2.63
2017-08-25 10:01:00 2017-08-25 10:32:00 20.92
2017-08-25 20:40:00 2017-08-25 21:45:00 17.41
2017-08-25 21:49:00 2017-08-25 22:14:00 16.02
2017-08-26 00:10:00 2017-08-26 02:14:00 29.77
2017-08-26 16:31:00 2017-08-26 16:55:00 7.15
2017-08-26 17:54:00 2017-08-26 18:19:00 10
2017-08-26 20:07:00 2017-08-26 20:08:00 0.19
2017-08-26 20:08:00 2017-08-26 20:11:00 1.35
2017-08-27 12:39:00 2017-08-27 12:54:00 1
2017-08-27 12:55:00 2017-08-27 13:48:00 9.29
2017-08-27 14:00:00 2017-08-27 14:34:00 3.86
2017-08-27 15:56:00 2017-08-27 16:37:00 10.45
2017-08-27 16:44:00 2017-08-27 16:51:00 1.8
2017-08-27 16:55:00 2017-08-27 17:00:00 0.68
2017-08-27 17:04:00 2017-08-27 17:19:00 4.96
2017-08-27 17:28:00 2017-08-27 17:39:00 2.33
2017-08-27 17:47:00 2017-08-27 18:58:00 24.19
2017-08-27 22:17:00 2017-08-27 22:41:00 16.24
2017-08-28 00:33:00 2017-08-28 01:22:00 13.62
2017-08-28 12:48:00 2017-08-28 12:51:00 0.47
2017-08-28 14:01:00 2017-08-28 14:03:00 0.4
2017-08-28 14:12:00 2017-08-28 15:31:00 34.86
2017-08-28 15:56:00 2017-08-28 17:04:00 34.47
2017-08-28 22:15:00 2017-08-28 22:38:00 18.57
2017-08-29 01:42:00 2017-08-29 02:05:00 18.88
2017-08-29 11:40:00 2017-08-29 11:44:00 1.04
2017-08-29 11:48:00 2017-08-29 12:09:00 0.03
2017-08-29 12:18:00 2017-08-29 12:21:00 0.03
2017-08-29 12:26:00 2017-08-29 12:32:00 1.05
2017-08-29 12:35:00 2017-08-29 13:15:00 24.05
2017-08-29 19:40:00 2017-08-29 19:42:00 0.35
2017-08-29 19:50:00 2017-08-29 20:19:00 27.72
2017-08-29 20:25:00 2017-08-29 20:41:00 10.42
2017-08-30 10:00:00 2017-08-30 10:47:00 24.25
2017-08-30 14:31:00 2017-08-30 14:56:00 1.68
2017-08-30 17:19:00 2017-08-30 17:43:00 0.04
2017-08-30 17:43:00 2017-08-30 17:50:00 0.29
2017-08-30 17:56:00 2017-08-30 18:40:00 16.85
2017-08-30 22:57:00 2017-08-30 23:35:00 17.31
2017-08-31 11:30:00 2017-08-31 11:41:00 0.43
2017-08-31 14:04:00 2017-08-31 14:06:00 0.41
2017-08-31 14:24:00 2017-08-31 14:26:00 0.68
2017-08-31 14:31:00 2017-08-31 15:42:00 34.88
2017-08-31 16:01:00 2017-08-31 17:07:00 30.45
2017-08-31 20:54:00 2017-08-31 21:21:00 19.6
2017-09-01 10:30:00 2017-09-01 10:59:00 17.63
2017-09-01 14:07:00 2017-09-01 15:07:00 27.45
2017-09-01 17:17:00 2017-09-01 17:36:00 1.93
2017-09-01 18:16:00 2017-09-01 19:19:00 20.58
2017-09-01 19:25:00 2017-09-01 19:38:00 4.8
2017-09-01 21:30:00 2017-09-01 21:54:00 1.94
2017-09-02 15:46:00 2017-09-02 16:06:00 0.99
2017-09-02 16:13:00 2017-09-02 16:16:00 1.01
2017-09-02 16:56:00 2017-09-02 16:59:00 0.42
2017-09-02 17:04:00 2017-09-02 17:06:00 0.4
2017-09-02 22:52:00 2017-09-02 22:54:00 0.07
2017-09-02 22:55:00 2017-09-02 23:15:00 18.62
2017-09-03 01:46:00 2017-09-03 02:10:00 18.9
2017-09-03 14:49:00 2017-09-03 15:04:00 3.14
2017-09-03 15:50:00 2017-09-03 16:07:00 10.17
2017-09-03 16:21:00 2017-09-03 16:38:00 7.79
2017-09-03 16:47:00 2017-09-03 16:52:00 1.11
2017-09-03 18:32:00 2017-09-03 18:37:00 1.2
2017-09-03 18:37:00 2017-09-03 18:44:00 0.91
2017-09-04 15:50:00 2017-09-04 15:54:00 0.42
2017-09-04 15:59:00 2017-09-04 16:11:00 2.3
2017-09-04 16:21:00 2017-09-04 16:43:00 8.31
2017-09-04 17:05:00 2017-09-04 17:15:00 2.54
2017-09-04 17:26:00 2017-09-04 17:41:00 4.52
2017-09-04 17:49:00 2017-09-04 18:25:00 29.55
2017-09-04 19:36:00 2017-09-04 19:51:00 0.93
2017-09-04 19:54:00 2017-09-04 19:59:00 0.5
2017-09-04 21:21:00 2017-09-04 21:55:00 29.37
2017-09-05 11:08:00 2017-09-05 11:51:00 35.5
2017-09-05 12:36:00 2017-09-05 13:07:00 2.29
2017-09-05 13:19:00 2017-09-05 13:22:00 0.51
2017-09-05 13:26:00 2017-09-05 14:03:00 33.09
2017-09-05 14:13:00 2017-09-05 15:01:00 24.03
2017-09-05 17:33:00 2017-09-05 18:11:00 14.55
2017-09-05 19:01:00 2017-09-05 19:19:00 11.31
2017-09-06 09:21:00 2017-09-06 09:39:00 7.73
2017-09-06 10:14:00 2017-09-06 10:30:00 7.75
2017-09-06 10:37:00 2017-09-06 11:13:00 24.13
2017-09-06 16:48:00 2017-09-06 17:35:00 25.3
2017-09-06 17:49:00 2017-09-06 17:55:00 0.18
2017-09-06 17:58:00 2017-09-06 18:00:00 0.39
2017-09-06 18:38:00 2017-09-06 19:04:00 15.93
2017-09-06 23:45:00 2017-09-07 00:14:00 19.45
2017-09-07 00:26:00 2017-09-07 00:30:00 1.01
2017-09-07 10:42:00 2017-09-07 11:35:00 31.74
2017-09-07 14:04:00 2017-09-07 14:39:00 27.38
2017-09-07 14:43:00 2017-09-07 14:52:00 3.06
2017-09-07 14:54:00 2017-09-07 16:00:00 32.96
2017-09-07 16:32:00 2017-09-07 16:33:00 0.07
2017-09-07 16:38:00 2017-09-07 17:04:00 2.31
2017-09-07 17:23:00 2017-09-07 18:14:00 33.03
2017-09-08 10:02:00 2017-09-08 10:30:00 19.73
2017-09-08 18:09:00 2017-09-08 18:37:00 18.97
2017-09-08 19:04:00 2017-09-08 19:18:00 1.87
2017-09-09 02:25:00 2017-09-09 02:28:00 1.1
2017-09-09 02:33:00 2017-09-09 02:35:00 1.05
2017-09-10 17:09:00 2017-09-10 17:44:00 14.25
2017-09-10 22:50:00 2017-09-10 22:53:00 0.25
2017-09-10 22:56:00 2017-09-10 22:57:00 0.02
2017-09-10 23:00:00 2017-09-10 23:23:00 16.18
2017-09-11 00:01:00 2017-09-11 00:19:00 1.83
2017-09-11 09:59:00 2017-09-11 10:06:00 1.91
2017-09-11 10:12:00 2017-09-11 10:51:00 27.49
2017-09-11 13:39:00 2017-09-11 14:13:00 27.23
2017-09-11 14:31:00 2017-09-11 15:31:00 35.45
2017-09-11 16:03:00 2017-09-11 17:09:00 36.01
2017-09-11 17:39:00 2017-09-11 18:01:00 9.88
2017-09-11 23:01:00 2017-09-11 23:05:00 1.14
2017-09-11 23:16:00 2017-09-11 23:30:00 5.93
2017-09-11 23:30:00 2017-09-11 23:54:00 4.94
2017-09-12 02:56:00 2017-09-12 04:00:00 25.87
2017-09-12 10:06:00 2017-09-12 10:46:00 24.84
2017-09-12 16:33:00 2017-09-12 17:20:00 22.43
2017-09-12 19:38:00 2017-09-12 20:14:00 21.79
2017-09-13 06:24:00 2017-09-13 06:59:00 25.84
2017-09-13 07:02:00 2017-09-13 07:14:00 5.77
2017-09-13 11:14:00 2017-09-13 11:36:00 16.26
2017-09-13 16:01:00 2017-09-13 16:57:00 24.79
2017-09-13 17:07:00 2017-09-13 17:48:00 15.94
2017-09-13 23:13:00 2017-09-13 23:35:00 16.73
2017-09-14 12:00:00 2017-09-14 12:27:00 19.71
2017-09-14 12:28:00 2017-09-14 12:30:00 0.18
2017-09-14 14:36:00 2017-09-14 15:06:00 14.98
2017-09-14 15:11:00 2017-09-14 15:17:00 2.99
2017-09-14 15:26:00 2017-09-14 16:44:00 37.48
2017-09-14 17:03:00 2017-09-14 18:17:00 34.18
2017-09-14 18:32:00 2017-09-14 18:41:00 3.03
2017-09-15 10:25:00 2017-09-15 10:26:00 0.05
2017-09-15 10:45:00 2017-09-15 10:48:00 0.29
2017-09-15 10:59:00 2017-09-15 11:05:00 0.3
2017-09-15 11:09:00 2017-09-15 11:36:00 10.82
2017-09-15 13:00:00 2017-09-15 13:17:00 8.37
2017-09-15 13:36:00 2017-09-15 14:30:00 25.19
2017-09-15 14:37:00 2017-09-15 15:01:00 0.45
2017-09-15 15:04:00 2017-09-15 16:59:00 85.51
2017-09-15 17:06:00 2017-09-15 18:57:00 129.72
2017-09-15 19:03:00 2017-09-15 20:02:00 60.96
2017-09-16 10:18:00 2017-09-16 10:39:00 16.04
2017-09-16 11:52:00 2017-09-16 12:12:00 16.68
2017-09-16 12:28:00 2017-09-16 13:29:00 49
2017-09-16 18:36:00 2017-09-16 19:30:00 45.7
2017-09-16 19:39:00 2017-09-16 19:47:00 2.1
2017-09-17 13:32:00 2017-09-17 13:41:00 2.24
2017-09-17 14:19:00 2017-09-17 14:48:00 14.68
2017-09-17 18:25:00 2017-09-17 18:26:00 0.05
2017-09-17 18:36:00 2017-09-17 19:03:00 12.26
2017-09-18 07:52:00 2017-09-18 08:03:00 2.04
2017-09-18 08:21:00 2017-09-18 08:56:00 37.94
2017-09-18 09:01:00 2017-09-18 09:53:00 65.7
2017-09-18 10:04:00 2017-09-18 10:34:00 39.43
2017-09-18 10:46:00 2017-09-18 11:07:00 14.25
2017-09-18 11:19:00 2017-09-18 13:29:00 138.98
2017-09-18 14:24:00 2017-09-18 14:26:00 0.04
2017-09-18 14:28:00 2017-09-18 15:23:00 35.52
2017-09-18 15:53:00 2017-09-18 17:49:00 36.64
2017-09-19 09:24:00 2017-09-19 10:22:00 24.37
2017-09-19 15:55:00 2017-09-19 16:53:00 15.87
2017-09-19 16:53:00 2017-09-19 17:20:00 0.85
2017-09-19 17:33:00 2017-09-19 18:06:00 10.95
2017-09-19 18:10:00 2017-09-19 18:34:00 8.41
2017-09-19 21:06:00 2017-09-19 21:10:00 1.24
2017-09-19 21:17:00 2017-09-19 21:21:00 1.05
2017-09-20 11:12:00 2017-09-20 11:16:00 1.22
2017-09-20 11:18:00 2017-09-20 11:59:00 24.15
2017-09-20 17:20:00 2017-09-20 18:07:00 24.15
2017-09-20 18:50:00 2017-09-20 19:17:00 16.02
2017-09-20 22:05:00 2017-09-20 22:32:00 17.5
2017-09-21 13:38:00 2017-09-21 13:44:00 0.72
2017-09-21 13:50:00 2017-09-21 15:26:00 35.81
2017-09-21 15:59:00 2017-09-21 16:15:00 8.26
2017-09-21 16:19:00 2017-09-21 17:32:00 28.1
2017-09-21 18:49:00 2017-09-21 19:25:00 16.05
2017-09-21 22:30:00 2017-09-21 22:59:00 16.97
2017-09-22 10:19:00 2017-09-22 10:21:00 0.43
2017-09-22 10:25:00 2017-09-22 10:26:00 0.4
2017-09-22 10:30:00 2017-09-22 10:54:00 19.15
2017-09-22 11:58:00 2017-09-22 12:02:00 1.05
2017-09-22 18:32:00 2017-09-22 18:59:00 20.95
2017-09-23 08:34:00 2017-09-23 08:51:00 1.15
2017-09-23 09:19:00 2017-09-23 10:31:00 37.57
2017-09-23 11:09:00 2017-09-23 11:23:00 5.67
2017-09-23 11:51:00 2017-09-23 12:15:00 4.64
2017-09-23 12:47:00 2017-09-23 13:40:00 8.45
2017-09-23 13:56:00 2017-09-23 15:08:00 34.62
2017-09-23 15:37:00 2017-09-23 16:07:00 1.56
2017-09-24 14:59:00 2017-09-24 15:02:00 0.43
2017-09-24 15:14:00 2017-09-24 17:09:00 6.6
2017-09-24 17:37:00 2017-09-24 18:01:00 7.05
2017-09-24 18:05:00 2017-09-24 18:07:00 0.41
2017-09-24 19:35:00 2017-09-24 20:31:00 25.28
2017-09-25 00:24:00 2017-09-25 00:26:00 0.42
2017-09-25 00:30:00 2017-09-25 01:10:00 23.13
2017-09-25 12:12:00 2017-09-25 12:38:00 19.45
2017-09-25 14:22:00 2017-09-25 14:50:00 19.86
2017-09-25 14:52:00 2017-09-25 15:54:00 35.53
2017-09-25 16:37:00 2017-09-25 18:17:00 34.54
2017-09-25 20:36:00 2017-09-25 21:08:00 28.91
2017-09-26 01:46:00 2017-09-26 02:21:00 26.32
2017-09-26 09:36:00 2017-09-26 10:18:00 24.02
2017-09-26 14:05:00 2017-09-26 14:39:00 25.3
2017-09-26 15:49:00 2017-09-26 15:58:00 1.53
2017-09-26 16:15:00 2017-09-26 16:22:00 1.1
2017-09-27 09:15:00 2017-09-27 10:16:00 24.76
2017-09-27 16:26:00 2017-09-27 17:49:00 35.87
2017-09-27 17:58:00 2017-09-27 18:46:00 27.64
2017-09-27 18:51:00 2017-09-27 18:59:00 2.08
2017-09-27 19:10:00 2017-09-27 20:17:00 21.17
2017-09-27 20:25:00 2017-09-27 21:56:00 3.6
2017-09-27 22:04:00 2017-09-27 22:32:00 16.56
2017-09-28 06:46:00 2017-09-28 07:19:00 14.4
2017-09-28 09:05:00 2017-09-28 09:29:00 8.06
2017-09-28 10:41:00 2017-09-28 11:21:00 22.34
2017-09-28 14:26:00 2017-09-28 16:05:00 35.57
2017-09-28 16:09:00 2017-09-28 16:21:00 1.17
2017-09-28 20:37:00 2017-09-28 20:40:00 1.1
2017-09-28 20:56:00 2017-09-28 21:00:00 1.15
2017-09-29 09:32:00 2017-09-29 10:02:00 19.73
I'd like to plot these discrete events the same way the below plots do, but where 2pi is one week rather than 24 hours in order to illuminate the periodicity of these events, where color represents distance.
I've attempted modifying the solution linked at the beginning of this question, but it hasn't gotten me anywhere. My new approach is to modify this solution, but I'm having a difficult time getting anything but horizontal and vertical lines scattered about a spiral. Making them curve and display in the correct locations is tough.
I'm open to any approach that successfully displays the data in a spiral plot without quantizing/binning it into specific intervals but rather allows the intervals themselves to describe discrete events along a continuous spiralling timeline. Likewise, I'm not interested in converting this to a raw single-point time series format where I'd have a great deal of data representing the time between trips. I'd like to achieve this in a temporal format (one that describes a time window rather than an event at a particular time).
Still needs work, but it's a start, with python and matplotlib.
The idea is to plot a spiral timeline in polar coordinates with 1 week period, each event is an arc of this spiral with a color depending on dist data.
There are lots of overlapping intervals though that this visualization tends to hide... maybe semitransparent arcs could be better, with a carefully chosen colormap.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patheffects as mpe
import pandas as pd
# styling
LINEWIDTH=4
EDGEWIDTH=1
CAPSTYLE="projecting"
COLORMAP="viridis_r"
ALPHA=1
FIRSTDAY=6 # 0=Mon, 6=Sun
# load dataset and parse timestamps
df = pd.read_csv('trips.csv')
df[['trip_start', 'trip_stop']] = df[['trip_start', 'trip_stop']].apply(pd.to_datetime)
# set origin at the first FIRSTDAY before the first trip, midnight
first_trip = df['trip_start'].min()
origin = (first_trip - pd.to_timedelta(first_trip.weekday() - FIRSTDAY, unit='d')).replace(hour=0, minute=0, second=0)
weekdays = pd.date_range(origin, origin + np.timedelta64(1, 'W')).strftime("%a").tolist()
# # convert trip timestamps to week fractions
df['start'] = (df['trip_start'] - origin) / np.timedelta64(1, 'W')
df['stop'] = (df['trip_stop'] - origin) / np.timedelta64(1, 'W')
# sort dataset so shortest trips are plotted last
# should prevent longer events to cover shorter ones, still suboptimal
df = df.sort_values('dist', ascending=False).reset_index()
fig = plt.figure(figsize=(8, 6))
ax = fig.gca(projection="polar")
for idx, event in df.iterrows():
# sample normalized distance from colormap
ndist = event['dist'] / df['dist'].max()
color = plt.cm.get_cmap(COLORMAP)(ndist)
tstart, tstop = event.loc[['start', 'stop']]
# timestamps are in week fractions, 2pi is one week
nsamples = int(1000. * (tstop - tstart))
t = np.linspace(tstart, tstop, nsamples)
theta = 2 * np.pi * t
arc, = ax.plot(theta, t, lw=LINEWIDTH, color=color, solid_capstyle=CAPSTYLE, alpha=ALPHA)
if EDGEWIDTH > 0:
arc.set_path_effects([mpe.Stroke(linewidth=LINEWIDTH+EDGEWIDTH, foreground='black'), mpe.Normal()])
# grid and labels
ax.set_rticks([])
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.set_xticks(np.linspace(0, 2*np.pi, 7, endpoint=False))
ax.set_xticklabels(weekdays)
ax.tick_params('x', pad=2)
ax.grid(True)
# setup a custom colorbar, everything's always a bit tricky with mpl colorbars
vmin = df['dist'].min()
vmax = df['dist'].max()
norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
sm = plt.cm.ScalarMappable(cmap=COLORMAP, norm=norm)
sm.set_array([])
plt.colorbar(sm, ticks=np.linspace(vmin, vmax, 10), fraction=0.04, aspect=60, pad=0.1, label="distance", ax=ax)
plt.savefig("spiral.png", pad_inches=0, bbox_inches="tight")
Full timeline
To see it's a spiral that never overlaps and it works for longer events too you can plot the full timeline (here with LINEWIDTH=3.5 to limit moiré fringing).
fullt = np.linspace(df['start'].min(), df['stop'].max(), 10000)
theta = 2 * np.pi * fullt
ax.plot(theta, fullt, lw=LINEWIDTH,
path_effects=[mpe.Stroke(linewidth=LINEWIDTH+LINEBORDER, foreground='black'), mpe.Normal()])
Example with a random set...
Here's the plot for a random dataset of 200 mainly short trips with the occasional 1 to 2 weeks long ones.
N = 200
df = pd.DataFrame()
df["start"] = np.random.uniform(0, 20, size=N)
df["stop"] = df["start"] + np.random.choice([np.random.uniform(0, 0.1),
np.random.uniform(1., 2.)], p=[0.98, 0.02], size=N)
df["dist"] = np.random.random(size=N)
... and different styles
inferno_r color map, rounded or butted linecaps, semitransparent, bolder edges, etc (click for full size)
Here's a start. Let me know if this is what you had in mind.
I began with your data sample and put trip_start and trip_stop into POSIXct format before continuing with the code below.
library(tidyverse)
library(lubridate)
dat = dat %>%
mutate(start=(hour(trip_start)*60 + minute(trip_start) + second(trip_start))/(24*60) + wday(trip_start),
stop=(hour(trip_stop)*60 + minute(trip_stop) + second(trip_stop))/(24*60) + wday(trip_stop),
tod = case_when(hour(trip_start) < 6 ~ "night",
hour(trip_start) < 12 ~ "morning",
hour(trip_start) < 18 ~ "afternoon",
hour(trip_start) < 24 ~ "evening"))
ggplot(dat) +
geom_segment(aes(x=start, xend=stop,
y=trip_start,
yend=trip_stop,
colour=tod),
size=5, show.legend = FALSE) +
coord_polar() +
scale_y_datetime(breaks=seq(as.POSIXct("2017-09-01"), as.POSIXct("2017-12-31"), by="week")) +
scale_x_continuous(limits=c(1,8), breaks=1:7,
labels=weekdays(x=as.Date(seq(7)+2, origin="1970-01-01"),
abbreviate=TRUE))+
expand_limits(y=as.POSIXct("2017-08-25")) +
theme_bw() +
scale_colour_manual(values=c(night="black", morning="orange",
afternoon="orange", evening="blue")) +
labs(x="",y="")
This could be achieved relatively straightforwardly with d3. I'll use your data to create a rough template of one basic possible approach. Here's what the result of this approach might look like:
The key ingredient is d3's radial line component that lets us define a line by plotting angle and radius (here's a recent answer showing another spiral graph, that answer started me down the path on this answer).
All we need to do is scale angle and radius to be able to use this effectively (for which we need the first time and last time in the dataset):
var angle = d3.scaleTime()
.domain([start,end])
.range([0,Math.PI * 2 * numberWeeks])
var radius = d3.scaleTime()
.domain([start,end])
.range([minInnerRadius,maxOuterRadius])
And from there we can create a spiral quite easily, we sample some dates throughout the interval and then pass them to the radial line function:
var spiral = d3.radialLine()
.curve(d3.curveCardinal)
.angle(angle)
.radius(radius);
Here's a quick demonstration of just the spiral covering your time period. I'm assuming a base familiarity with d3 for this answer, so have not touched on a few parts of the code.
Once we have that, it's just a matter of adding sections from the data. The most simple way would be to plainly draw a stroke with some width and color it appropriately. This requires the same as above, but rather than sampling points from the start and end times of the dataset, we just need the start and end times of each datum:
// append segments on spiral:
var segments = g.selectAll()
.data(data)
.enter()
.append("path")
.attr("d", function(d) {
return /* sample points and feed to spiral function here */;
})
.style("stroke-width", /* appropriate width here */ )
.style("stroke",function(d) { return /* color logic here */ })
This might look something like this (with data mouseover).
This is just a proof of concept, if you were looking for more control and a nicer look, you could create a polygonal path for each data entry and use both fill & stroke. As is, you'll have to make do with layering strokes to get borders if desired and svg manipulations like line capping options.
Also, as it's d3, and longer timespans may be hard to show all at once, you could show less time but rotate the spiral so that it animates through your time span, dropping off events at the end and creating them in the origin. The actual chart might need to be canvas for this to happen smoothly depending on number of nodes, but to convert to canvas is relatively trivial in this case.
For the sake of filling out the visualization a little with a legend and day labels, this is what I have.

Select certain dates from Pandas dataframe

I am learning how to filter dates on a Pandas data frame and need some help with the following please. This is my original data frame (from this data):
data
Out[120]:
Open High Low Last Volume NumberOfTrades BidVolume AskVolume
Timestamp
2014-03-04 09:30:00 1783.50 1784.50 1783.50 1784.50 171 17 29 142
2014-03-04 09:31:00 1784.75 1785.75 1784.50 1785.25 28 21 10 18
2014-03-04 09:32:00 1785.00 1786.50 1785.00 1786.50 81 19 4 77
2014-03-04 09:33:00 1786.00 1786.00 1785.25 1785.25 41 14 8 33
2014-03-04 09:34:00 1785.00 1785.25 1784.75 1785.25 11 8 2 9
2014-03-04 09:35:00 1785.50 1786.75 1785.50 1785.75 49 27 13 36
2014-03-04 09:36:00 1786.00 1786.00 1785.25 1785.75 12 8 3 9
2014-03-04 09:37:00 1786.00 1786.25 1785.25 1785.25 15 8 10 5
2014-03-04 09:38:00 1785.50 1785.50 1784.75 1785.25 24 17 17 7
data.dtypes
Out[118]:
Open float64
High float64
Low float64
Last float64
Volume int64
NumberOfTrades int64
BidVolume int64
AskVolume int64
dtype: object
I then resampled to 5 minute sections:
five_min = data.resample('5T').sum()
And look for the high volume days:
max_volume = five_min.Volume.at_time('9:30') > 65000
I then try to get the days high volume days as follows:
five_min.Volume = max_volume[max_volume == True]
for_high_vol = five_min.Volume.dropna()
for_high_vol
Timestamp
2014-03-21 09:30:00 True
2014-04-11 09:30:00 True
2014-04-16 09:30:00 True
2014-04-17 09:30:00 True
2014-07-18 09:30:00 True
2014-07-31 09:30:00 True
2014-09-19 09:30:00 True
2014-10-07 09:30:00 True
2014-10-10 09:30:00 True
2014-10-14 09:30:00 True
2014-10-15 09:30:00 True
2014-10-16 09:30:00 True
2014-10-17 09:30:00 True
I would like to use the index from "for_high_vol" to select all of the days from the original "data" Pandas dataframe.
Im sure there are much better was to approach this so can someone please show me the simplest way to do this?
IIUC, you can do it this way:
x.ix[(x.groupby(pd.Grouper(key='Timestamp', freq='5T'))['Volume'].transform('sum') > 65000)
&
(x.Timestamp.dt.hour==9)
&
(x.Timestamp.dt.minute>=30) & (x.Timestamp.dt.minute<=34)]
in order to set index back:
x.ix[(x.groupby(pd.Grouper(key='Timestamp', freq='5T'))['Volume'].transform('sum') > 65000)
&
(x.Timestamp.dt.hour==9)
&
(x.Timestamp.dt.minute>=30) & (x.Timestamp.dt.minute<=34)].set_index('Timestamp')
PS Timestamp is a regular column in my DF, not an index
Explanation:
resample / group our DF by 5 minutes interval, calculate the sum of Volume for each group and assign this sum to all rows in the group. For example in the example below 332 - is the sum of Volume in the first 5-min group
In [41]: (x.groupby(pd.Grouper(key='Timestamp', freq='5T'))['Volume'].transform('sum')).head(10)
Out[41]:
0 332
1 332
2 332
3 332
4 332
5 113
6 113
7 113
8 113
9 113
dtype: int64
filter time - the conditions are self-explanatory:
(x.Timestamp.dt.hour==9) & (x.Timestamp.dt.minute>=30) & (x.Timestamp.dt.minute<=34)].set_index('Timestamp')
and finally combine all conditions (filters) together - pass it to .ix[] indexer and set index back to Timestamp:
x.ix[(x.groupby(pd.Grouper(key='Timestamp', freq='5T'))['Volume'].transform('sum') > 65000)
&
(x.Timestamp.dt.hour==9)
&
(x.Timestamp.dt.minute>=30) & (x.Timestamp.dt.minute<=34)].set_index('Timestamp')
Output:
Out[32]:
Timestamp Open High Low Last Volume NumberOfTrades BidVolume AskVolume
5011 2014-03-21 09:30:00 1800.75 1802.50 1800.00 1802.25 30181 6006 13449 16732
5012 2014-03-21 09:31:00 1802.50 1803.25 1802.25 1802.50 15588 3947 5782 9806
5013 2014-03-21 09:32:00 1802.50 1803.75 1802.25 1803.25 16409 3994 6867 9542
5014 2014-03-21 09:33:00 1803.00 1803.50 1802.75 1803.25 10790 3158 4781 6009
5015 2014-03-21 09:34:00 1803.25 1804.75 1803.25 1804.75 13377 3466 4690 8687
11086 2014-04-11 09:30:00 1744.75 1744.75 1743.00 1743.50 21504 5876 11178 10326
11087 2014-04-11 09:31:00 1743.50 1746.50 1743.25 1746.00 21582 6191 8830 12752
11088 2014-04-11 09:32:00 1746.00 1746.50 1744.25 1745.75 18961 5214 9521 9440
11089 2014-04-11 09:33:00 1746.00 1746.25 1744.00 1744.25 12832 3658 7219 5613
11090 2014-04-11 09:34:00 1744.25 1744.25 1742.00 1742.75 15478 4919 8912 6566
12301 2014-04-16 09:30:00 1777.50 1778.25 1776.25 1777.00 21178 5431 10775 10403
12302 2014-04-16 09:31:00 1776.75 1779.25 1776.50 1778.50 16456 4400 6351 10105
12303 2014-04-16 09:32:00 1778.50 1779.25 1777.25 1777.50 9956 3015 5810 4146
12304 2014-04-16 09:33:00 1777.50 1778.00 1776.25 1776.25 8724 2470 5326 3398
12305 2014-04-16 09:34:00 1776.25 1777.00 1775.50 1776.25 9566 2968 5098 4468
12706 2014-04-17 09:30:00 1781.50 1782.50 1781.25 1782.25 16474 4583 7510 8964
12707 2014-04-17 09:31:00 1782.25 1782.50 1781.00 1781.25 10328 2587 6310 4018
12708 2014-04-17 09:32:00 1781.25 1782.25 1781.00 1781.25 9072 2142 4618 4454
12709 2014-04-17 09:33:00 1781.00 1781.75 1780.25 1781.25 17866 3807 10665 7201
12710 2014-04-17 09:34:00 1781.50 1782.25 1780.50 1781.75 11322 2523 5538 5784
38454 2014-07-18 09:30:00 1893.50 1893.75 1892.50 1893.00 24864 5135 13874 10990
38455 2014-07-18 09:31:00 1892.75 1893.50 1892.75 1892.75 8003 1751 3571 4432
38456 2014-07-18 09:32:00 1893.00 1893.50 1892.75 1893.50 7062 1680 3454 3608
38457 2014-07-18 09:33:00 1893.25 1894.25 1893.00 1894.25 10581 1955 3925 6656
38458 2014-07-18 09:34:00 1894.25 1895.25 1894.00 1895.25 15309 3347 5516 9793
42099 2014-07-31 09:30:00 1886.25 1886.25 1884.25 1884.75 21668 5857 11910 9758
42100 2014-07-31 09:31:00 1884.50 1884.75 1882.25 1883.00 17487 5186 11403 6084
42101 2014-07-31 09:32:00 1883.00 1884.50 1882.50 1884.00 13174 3782 4791 8383
42102 2014-07-31 09:33:00 1884.25 1884.50 1883.00 1883.25 9095 2814 5299 3796
42103 2014-07-31 09:34:00 1883.25 1884.25 1883.00 1884.25 7593 2528 3794 3799
... ... ... ... ... ... ... ... ... ...
193508 2016-01-21 09:30:00 1838.00 1838.75 1833.00 1834.00 22299 9699 12666 9633
193509 2016-01-21 09:31:00 1834.00 1836.50 1833.00 1834.50 8851 4520 4010 4841
193510 2016-01-21 09:32:00 1834.25 1835.25 1832.50 1833.25 7957 3672 3582 4375
193511 2016-01-21 09:33:00 1833.00 1838.50 1832.00 1838.00 12902 5564 5174 7728
193512 2016-01-21 09:34:00 1838.00 1841.50 1837.75 1840.50 13991 6130 6799 7192
199178 2016-02-10 09:30:00 1840.00 1841.75 1839.00 1840.75 13683 5080 6743 6940
199179 2016-02-10 09:31:00 1840.75 1842.00 1838.75 1841.50 11753 4623 5616 6137
199180 2016-02-10 09:32:00 1841.50 1844.75 1840.75 1843.00 16402 6818 8226 8176
199181 2016-02-10 09:33:00 1843.00 1843.50 1841.00 1842.00 14963 5402 8431 6532
199182 2016-02-10 09:34:00 1842.25 1843.50 1840.00 1840.00 8397 3475 4537 3860
200603 2016-02-16 09:30:00 1864.00 1866.25 1863.50 1864.75 19585 6865 9548 10037
200604 2016-02-16 09:31:00 1865.00 1865.50 1863.75 1864.25 16604 5936 8095 8509
200605 2016-02-16 09:32:00 1864.25 1864.75 1862.75 1863.50 10126 4713 5591 4535
200606 2016-02-16 09:33:00 1863.25 1863.75 1861.50 1862.25 9648 3786 5824 3824
200607 2016-02-16 09:34:00 1862.25 1863.50 1861.75 1862.25 10748 4143 5413 5335
205058 2016-03-02 09:30:00 1952.75 1954.25 1952.00 1952.75 19812 6684 10350 9462
205059 2016-03-02 09:31:00 1952.75 1954.50 1952.25 1953.50 10163 4236 3884 6279
205060 2016-03-02 09:32:00 1953.50 1954.75 1952.25 1952.50 15771 5519 8135 7636
205061 2016-03-02 09:33:00 1952.75 1954.50 1952.50 1953.75 9556 3583 3768 5788
205062 2016-03-02 09:34:00 1953.75 1954.75 1952.25 1952.50 11898 4463 6459 5439
209918 2016-03-18 09:30:00 2027.50 2028.25 2026.50 2028.00 38092 8644 17434 20658
209919 2016-03-18 09:31:00 2028.00 2028.25 2026.75 2027.25 11631 3209 6384 5247
209920 2016-03-18 09:32:00 2027.25 2027.75 2027.00 2027.50 9664 3270 5080 4584
209921 2016-03-18 09:33:00 2027.50 2027.75 2026.75 2026.75 10610 3117 5358 5252
209922 2016-03-18 09:34:00 2026.75 2027.00 2026.00 2026.50 8076 3022 4670 3406
227722 2016-05-20 09:30:00 2034.25 2035.25 2033.50 2034.50 30272 7815 16098 14174
227723 2016-05-20 09:31:00 2034.75 2035.75 2034.50 2035.50 12997 3690 6458 6539
227724 2016-05-20 09:32:00 2035.50 2037.50 2035.50 2037.25 12661 3864 5233 7428
227725 2016-05-20 09:33:00 2037.25 2037.75 2036.50 2037.00 9057 2524 5190 3867
227726 2016-05-20 09:34:00 2037.00 2037.50 2036.75 2037.00 5190 1620 2748 2442
[255 rows x 9 columns]

Categories

Resources