FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
feast/sdk/python/feast/driver_test_data.py at v0.60.0 · feast-dev/feast · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
feast-dev
/
feast
Public
Notifications
You must be signed in to change notification settings
Fork
1.4k
Star
7.2k
Code
Issues
215
Pull requests
190
Discussions
Actions
Security and quality
1
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
feast
/
sdk
/
python
/
feast
/
driver_test_data.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
321 lines (282 loc) · 13.6 KB
Breadcrumbs
feast
/
sdk
/
python
/
feast
/
driver_test_data.py
Copy path
File metadata and controls
321 lines (282 loc) · 13.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# This module generates dummy data to be used for tests and examples.
import
itertools
from
datetime
import
timedelta
,
timezone
from
enum
import
Enum
from
zoneinfo
import
ZoneInfo
import
numpy
as
np
import
pandas
as
pd
from
feast
.
infra
.
offline_stores
.
offline_utils
import
(
DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL
,
)
class
EventTimestampType
(
Enum
):
TZ_NAIVE
=
0
TZ_AWARE_UTC
=
1
TZ_AWARE_FIXED_OFFSET
=
2
TZ_AWARE_US_PACIFIC
=
3
def
_convert_event_timestamp
(
event_timestamp
:
pd
.
Timestamp
,
t
:
EventTimestampType
):
if
t
==
EventTimestampType
.
TZ_NAIVE
:
return
event_timestamp
elif
t
==
EventTimestampType
.
TZ_AWARE_UTC
:
return
event_timestamp
.
replace
(
tzinfo
=
timezone
.
utc
)
elif
t
==
EventTimestampType
.
TZ_AWARE_FIXED_OFFSET
:
return
event_timestamp
.
replace
(
tzinfo
=
timezone
.
utc
).
astimezone
(
tz
=
timezone
(
timedelta
(
minutes
=
60
))
)
elif
t
==
EventTimestampType
.
TZ_AWARE_US_PACIFIC
:
return
event_timestamp
.
replace
(
tzinfo
=
timezone
.
utc
).
astimezone
(
tz
=
ZoneInfo
(
"US/Pacific"
)
)
def
create_orders_df
(
customers
,
drivers
,
start_date
,
end_date
,
order_count
,
locations
=
None
,
)
->
pd
.
DataFrame
:
"""
Example df generated by this function (if locations):
| order_id | driver_id | customer_id | origin_id | destination_id | order_is_success | event_timestamp |
+----------+-----------+-------------+-----------+----------------+------------------+---------------------+
| 100 | 5004 | 1007 | 1 | 18 | 0 | 2021-03-10 19:31:15 |
| 101 | 5003 | 1006 | 24 | 42 | 0 | 2021-03-11 22:02:50 |
| 102 | 5010 | 1005 | 19 | 12 | 0 | 2021-03-13 00:34:24 |
| 103 | 5010 | 1001 | 35 | 8 | 1 | 2021-03-14 03:05:59 |
"""
df
=
pd
.
DataFrame
()
df
[
"order_id"
]
=
[
order_id
for
order_id
in
range
(
100
,
100
+
order_count
)]
df
[
"driver_id"
]
=
np
.
random
.
choice
(
drivers
,
order_count
)
df
[
"customer_id"
]
=
np
.
random
.
choice
(
customers
,
order_count
)
if
locations
:
location_pairs
=
np
.
array
(
list
(
itertools
.
permutations
(
locations
,
2
)))
locations_sample
=
location_pairs
[
np
.
random
.
choice
(
len
(
location_pairs
),
order_count
)
].
T
df
[
"origin_id"
]
=
locations_sample
[
0
]
df
[
"destination_id"
]
=
locations_sample
[
1
]
df
[
"order_is_success"
]
=
np
.
random
.
randint
(
0
,
2
,
size
=
order_count
).
astype
(
np
.
int32
)
df
[
DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL
]
=
[
_convert_event_timestamp
(
pd
.
Timestamp
(
dt
,
unit
=
"ms"
).
round
(
"ms"
),
EventTimestampType
(
idx
%
4
),
)
for
idx
,
dt
in
enumerate
(
pd
.
date_range
(
start
=
start_date
,
end
=
end_date
,
periods
=
order_count
,
tz
=
"UTC"
)
)
]
df
.
sort_values
(
by
=
[
DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL
,
"order_id"
,
"driver_id"
,
"customer_id"
,
],
inplace
=
True
,
)
return
df
def
create_driver_hourly_stats_df
(
drivers
,
start_date
,
end_date
)
->
pd
.
DataFrame
:
"""
Example df generated by this function:
| event_timestamp | driver_id | conv_rate | acc_rate | avg_daily_trips | created |
|------------------+-----------+-----------+----------+-----------------+------------------|
| 2021-03-17 19:31 | 5010 | 0.229297 | 0.685843 | 861 | 2021-03-24 19:34 |
| 2021-03-17 20:31 | 5010 | 0.781655 | 0.861280 | 769 | 2021-03-24 19:34 |
| 2021-03-17 21:31 | 5010 | 0.150333 | 0.525581 | 778 | 2021-03-24 19:34 |
| 2021-03-17 22:31 | 5010 | 0.951701 | 0.228883 | 570 | 2021-03-24 19:34 |
| 2021-03-17 23:31 | 5010 | 0.819598 | 0.262503 | 473 | 2021-03-24 19:34 |
| | ... | ... | ... | ... | |
| 2021-03-24 16:31 | 5001 | 0.061585 | 0.658140 | 477 | 2021-03-24 19:34 |
| 2021-03-24 17:31 | 5001 | 0.088949 | 0.303897 | 618 | 2021-03-24 19:34 |
| 2021-03-24 18:31 | 5001 | 0.096652 | 0.747421 | 480 | 2021-03-24 19:34 |
| 2021-03-17 19:31 | 5005 | 0.142936 | 0.707596 | 466 | 2021-03-24 19:34 |
| 2021-03-17 19:31 | 5005 | 0.142936 | 0.707596 | 466 | 2021-03-24 19:34 |
"""
df_hourly
=
pd
.
DataFrame
(
{
"event_timestamp"
: [
pd
.
Timestamp
(
dt
,
unit
=
"ms"
).
round
(
"ms"
)
for
dt
in
pd
.
date_range
(
start
=
start_date
,
end
=
end_date
,
freq
=
"1h"
,
inclusive
=
"left"
,
tz
=
"UTC"
,
)
]
# include a fixed timestamp for get_historical_features in the quickstart
+
[
pd
.
Timestamp
(
year
=
2021
,
month
=
4
,
day
=
12
,
hour
=
7
,
minute
=
0
,
second
=
0
,
tz
=
"UTC"
)
]
}
)
df_all_drivers
=
pd
.
DataFrame
()
for
driver
in
drivers
:
df_hourly_copy
=
df_hourly
.
copy
()
df_hourly_copy
[
"driver_id"
]
=
driver
df_all_drivers
=
pd
.
concat
([
df_hourly_copy
,
df_all_drivers
])
df_all_drivers
.
reset_index
(
drop
=
True
,
inplace
=
True
)
rows
=
df_all_drivers
[
"event_timestamp"
].
count
()
df_all_drivers
[
"conv_rate"
]
=
np
.
random
.
random
(
size
=
rows
).
astype
(
np
.
float32
)
df_all_drivers
[
"acc_rate"
]
=
np
.
random
.
random
(
size
=
rows
).
astype
(
np
.
float32
)
df_all_drivers
[
"avg_daily_trips"
]
=
np
.
random
.
randint
(
0
,
1000
,
size
=
rows
).
astype
(
np
.
int32
)
df_all_drivers
[
"created"
]
=
pd
.
to_datetime
(
pd
.
Timestamp
.
now
(
tz
=
None
).
round
(
"ms"
))
# Create duplicate rows that should be filtered by created timestamp
# TODO: These duplicate rows area indirectly being filtered out by the point in time join already. We need to
# inject a bad row at a timestamp where we know it will get joined to the entity dataframe, and then test that
# we are actually filtering it with the created timestamp
late_row
=
df_all_drivers
[
rows
//
2
:
rows
//
2
+
1
]
df_all_drivers
=
pd
.
concat
([
df_all_drivers
,
late_row
,
late_row
],
ignore_index
=
True
)
return
df_all_drivers
def
create_customer_daily_profile_df
(
customers
,
start_date
,
end_date
)
->
pd
.
DataFrame
:
"""
Example df generated by this function:
| event_timestamp | customer_id | current_balance | avg_passenger_count | lifetime_trip_count | created |
|------------------+-------------+-----------------+---------------------+---------------------+------------------|
| 2021-03-17 19:31 | 1010 | 0.889188 | 0.049057 | 412 | 2021-03-24 19:38 |
| 2021-03-18 19:31 | 1010 | 0.979273 | 0.212630 | 639 | 2021-03-24 19:38 |
| 2021-03-19 19:31 | 1010 | 0.976549 | 0.176881 | 70 | 2021-03-24 19:38 |
| 2021-03-20 19:31 | 1010 | 0.273697 | 0.325012 | 68 | 2021-03-24 19:38 |
| 2021-03-21 19:31 | 1010 | 0.438262 | 0.313009 | 192 | 2021-03-24 19:38 |
| | ... | ... | ... | ... | |
| 2021-03-19 19:31 | 1001 | 0.738860 | 0.857422 | 344 | 2021-03-24 19:38 |
| 2021-03-20 19:31 | 1001 | 0.848397 | 0.745989 | 106 | 2021-03-24 19:38 |
| 2021-03-21 19:31 | 1001 | 0.301552 | 0.185873 | 812 | 2021-03-24 19:38 |
| 2021-03-22 19:31 | 1001 | 0.943030 | 0.561219 | 322 | 2021-03-24 19:38 |
| 2021-03-23 19:31 | 1001 | 0.354919 | 0.810093 | 273 | 2021-03-24 19:38 |
"""
df_daily
=
pd
.
DataFrame
(
{
"event_timestamp"
: [
pd
.
Timestamp
(
dt
,
unit
=
"ms"
).
round
(
"ms"
)
for
dt
in
pd
.
date_range
(
start
=
start_date
,
end
=
end_date
,
freq
=
"1D"
,
inclusive
=
"left"
,
tz
=
"UTC"
,
)
]
}
)
df_all_customers
=
pd
.
DataFrame
()
for
customer
in
customers
:
df_daily_copy
=
df_daily
.
copy
()
df_daily_copy
[
"customer_id"
]
=
customer
df_all_customers
=
pd
.
concat
([
df_daily_copy
,
df_all_customers
])
df_all_customers
.
reset_index
(
drop
=
True
,
inplace
=
True
)
rows
=
df_all_customers
[
"event_timestamp"
].
count
()
df_all_customers
[
"current_balance"
]
=
np
.
random
.
random
(
size
=
rows
).
astype
(
np
.
float32
)
df_all_customers
[
"avg_passenger_count"
]
=
np
.
random
.
random
(
size
=
rows
).
astype
(
np
.
float32
)
df_all_customers
[
"lifetime_trip_count"
]
=
np
.
random
.
randint
(
0
,
1000
,
size
=
rows
).
astype
(
np
.
int32
)
# TODO: Remove created timestamp in order to test whether its really optional
df_all_customers
[
"created"
]
=
pd
.
to_datetime
(
pd
.
Timestamp
.
now
(
tz
=
None
).
round
(
"ms"
))
return
df_all_customers
def
create_location_stats_df
(
locations
,
start_date
,
end_date
)
->
pd
.
DataFrame
:
"""
Example df generated by this function:
| event_timestamp | location_id | temperature | created |
+------------------+-------------+-------------+------------------+
| 2021-03-17 19:31 | 1 | 74 | 2021-03-24 19:38 |
| 2021-03-17 20:31 | 24 | 63 | 2021-03-24 19:38 |
| 2021-03-17 21:31 | 19 | 65 | 2021-03-24 19:38 |
| 2021-03-17 22:31 | 35 | 86 | 2021-03-24 19:38 |
"""
df_hourly
=
pd
.
DataFrame
(
{
"event_timestamp"
: [
pd
.
Timestamp
(
dt
,
unit
=
"ms"
).
round
(
"ms"
)
for
dt
in
pd
.
date_range
(
start
=
start_date
,
end
=
end_date
,
freq
=
"1h"
,
inclusive
=
"left"
,
tz
=
"UTC"
,
)
]
}
)
df_all_locations
=
pd
.
DataFrame
()
for
location
in
locations
:
df_hourly_copy
=
df_hourly
.
copy
()
df_hourly_copy
[
"location_id"
]
=
location
df_all_locations
=
pd
.
concat
([
df_hourly_copy
,
df_all_locations
])
df_all_locations
.
reset_index
(
drop
=
True
,
inplace
=
True
)
rows
=
df_all_locations
[
"event_timestamp"
].
count
()
df_all_locations
[
"temperature"
]
=
np
.
random
.
randint
(
50
,
100
,
size
=
rows
).
astype
(
np
.
int32
)
# TODO: Remove created timestamp in order to test whether its really optional
df_all_locations
[
"created"
]
=
pd
.
to_datetime
(
pd
.
Timestamp
.
now
(
tz
=
None
).
round
(
"ms"
))
return
df_all_locations
def
create_global_daily_stats_df
(
start_date
,
end_date
)
->
pd
.
DataFrame
:
"""
Example df generated by this function:
| event_timestamp | num_rides | avg_ride_length | created |
|------------------+-------------+-----------------+------------------|
| 2021-03-17 19:00 | 99 | 0.889188 | 2021-03-24 19:38 |
| 2021-03-18 19:00 | 52 | 0.979273 | 2021-03-24 19:38 |
| 2021-03-19 19:00 | 66 | 0.976549 | 2021-03-24 19:38 |
| 2021-03-20 19:00 | 84 | 0.273697 | 2021-03-24 19:38 |
| 2021-03-21 19:00 | 89 | 0.438262 | 2021-03-24 19:38 |
| | ... | ... | |
| 2021-03-24 19:00 | 54 | 0.738860 | 2021-03-24 19:38 |
| 2021-03-25 19:00 | 58 | 0.848397 | 2021-03-24 19:38 |
| 2021-03-26 19:00 | 69 | 0.301552 | 2021-03-24 19:38 |
| 2021-03-27 19:00 | 63 | 0.943030 | 2021-03-24 19:38 |
| 2021-03-28 19:00 | 79 | 0.354919 | 2021-03-24 19:38 |
"""
df_daily
=
pd
.
DataFrame
(
{
"event_timestamp"
: [
pd
.
Timestamp
(
dt
,
unit
=
"ms"
,
).
round
(
"ms"
)
for
dt
in
pd
.
date_range
(
start
=
start_date
,
end
=
end_date
,
freq
=
"1D"
,
inclusive
=
"left"
,
tz
=
"UTC"
,
)
]
}
)
rows
=
df_daily
[
"event_timestamp"
].
count
()
df_daily
[
"num_rides"
]
=
np
.
random
.
randint
(
50
,
100
,
size
=
rows
).
astype
(
np
.
int32
)
df_daily
[
"avg_ride_length"
]
=
np
.
random
.
random
(
size
=
rows
).
astype
(
np
.
float32
)
# TODO: Remove created timestamp in order to test whether its really optional
df_daily
[
"created"
]
=
pd
.
to_datetime
(
pd
.
Timestamp
.
now
(
tz
=
None
).
round
(
"ms"
))
return
df_daily
def
create_field_mapping_df
(
start_date
,
end_date
)
->
pd
.
DataFrame
:
"""
Example df generated by this function:
| event_timestamp | column_name | created |
|------------------+-------------+------------------|
| 2021-03-17 19:00 | 99 | 2021-03-24 19:38 |
| 2021-03-17 19:00 | 22 | 2021-03-24 19:38 |
| 2021-03-17 19:00 | 7 | 2021-03-24 19:38 |
| 2021-03-17 19:00 | 45 | 2021-03-24 19:38 |
"""
size
=
10
df
=
pd
.
DataFrame
()
df
[
"column_name"
]
=
np
.
random
.
randint
(
1
,
100
,
size
=
size
).
astype
(
np
.
int32
)
df
[
DEFAULT_ENTITY_DF_EVENT_TIMESTAMP_COL
]
=
[
_convert_event_timestamp
(
pd
.
Timestamp
(
dt
,
unit
=
"ms"
).
round
(
"ms"
),
EventTimestampType
(
idx
%
4
),
)
for
idx
,
dt
in
enumerate
(
pd
.
date_range
(
start
=
start_date
,
end
=
end_date
,
periods
=
size
,
tz
=
"UTC"
)
)
]
df
[
"created"
]
=
pd
.
to_datetime
(
pd
.
Timestamp
.
now
(
tz
=
None
).
round
(
"ms"
))
return
df
Back
|
FazBrowse Home
|
New Git URL