Skip to content

Commit ac51b37

Browse files
committed
Merge branch 'examples' of github.com:InsightLab/PyMove into examples
2 parents 7383a0c + afbaeb2 commit ac51b37

File tree

2 files changed

+166
-8
lines changed

2 files changed

+166
-8
lines changed

pymove/tests/test_utils_datetime.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,24 @@ def test_str_to_datetime():
6565
assert(converted_date_time == expected_date_time)
6666

6767

68-
def test_to_str():
68+
def test_datetime_to_str():
6969

7070
expected = str_date_time_default
7171

7272
data = default_date_time
7373

74-
str_date_time = datetime.to_str(data)
74+
str_date_time = datetime.datetime_to_str(data)
7575

7676
assert(str_date_time == expected)
7777

7878

79-
def test_to_min():
79+
def test_datetime_to_min():
8080

8181
expected = 25347608
8282

8383
data = default_date_time
8484

85-
date_to_min = datetime.to_min(data)
85+
date_to_min = datetime.datetime_to_min(data)
8686

8787
assert(date_to_min == expected)
8888

@@ -145,7 +145,7 @@ def test_working_day():
145145

146146
def test_now_str():
147147

148-
expected = datetime.to_str(dt.datetime.now())
148+
expected = datetime.datetime_to_str(dt.datetime.now())
149149

150150
time_now = datetime.now_str()
151151

pymove/utils/datetime.py

Lines changed: 161 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ def date_to_str(dt: datetime) -> Text:
5858
str
5959
Represents the date in string format
6060
61+
Example
62+
-------
63+
import datatime
64+
time_now = datetime.datetime.now()
65+
66+
print(time_now)
67+
>>> 2021-04-29 11:01:29.909340
68+
print(type(time_now))
69+
>>> <class 'datetime.datetime'>
70+
71+
print(date_to_str(time_now), type(time_now))
72+
>>> 2021-04-29 <class 'str'>
73+
6174
"""
6275
return dt.strftime('%Y-%m-%d')
6376

@@ -76,14 +89,27 @@ def str_to_datetime(dt_str: Text) -> datetime:
7689
datetime
7790
Represents a datetime in datetime format
7891
92+
Example
93+
-------
94+
time_1 = '2020-06-29'
95+
time_2 = '2020-06-29 12:45:59'
96+
97+
print(type(time_1), type(time_2))
98+
>>> <class 'str'> <class 'str'>
99+
print( str_to_datetime(time_1), type(str_to_datetime(time_1)))
100+
>>> 2020-06-29 00:00:00 <class 'datetime.datetime'>
101+
print(str_to_datetime(time_2), type(str_to_datetime(time_2)))
102+
>>> 2020-06-29 12:45:59 <class 'datetime.datetime'>
103+
104+
79105
"""
80106
if len(dt_str) == 10:
81107
return datetime.strptime(dt_str, '%Y-%m-%d')
82108
else:
83109
return datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
84110

85111

86-
def to_str(dt: datetime) -> Text:
112+
def datetime_to_str(dt: datetime) -> Text:
87113
"""
88114
Converts a date in datetime format to string format.
89115
@@ -97,11 +123,24 @@ def to_str(dt: datetime) -> Text:
97123
str
98124
Represents a datetime in string format "%Y-%m-%d %H:%M:%S".
99125
126+
Example:
127+
-------
128+
import datetime
129+
time_now = datetime.datetime.now()
130+
131+
print(time_now)
132+
>>> 2021-04-29 14:15:29.708113
133+
print(type(time_now))
134+
>>> <class 'datetime.datetime'>
135+
136+
print(data_to_str(time_now), type(data_to_str(time_now)))
137+
>>> 2021-04-29 14:15:29 <class 'str' >
138+
100139
"""
101140
return dt.strftime('%Y-%m-%d %H:%M:%S')
102141

103142

104-
def to_min(dt: datetime) -> int:
143+
def datetime_to_min(dt: datetime) -> int:
105144
"""
106145
Converts a datetime to an int representation in minutes.
107146
@@ -117,6 +156,16 @@ def to_min(dt: datetime) -> int:
117156
int
118157
Represents minutes from
119158
159+
Example
160+
-------
161+
import datetime
162+
163+
time_now = datetime.datetime.now()
164+
print(type(to_min(time_now)))
165+
>>> <class 'int'>
166+
to_min(time_now)
167+
>>> 26996497
168+
120169
"""
121170
# get an integer time slot from a datetime
122171
return int(
@@ -140,6 +189,11 @@ def min_to_datetime(minutes: int) -> datetime:
140189
datetime
141190
Represents minutes in datetime format
142191
192+
Example
193+
-------
194+
print(min_to_datetime(26996497), type(min_to_datetime(26996497)))
195+
>>> 2021-04-30 13:37:00 <class 'datetime.datetime'>
196+
143197
"""
144198
return datetime.utcfromtimestamp(minutes * 60)
145199

@@ -158,6 +212,18 @@ def to_day_of_week_int(dt: datetime) -> int:
158212
int
159213
Represents day of week.
160214
215+
Example
216+
-------
217+
from pymove.utils.datetime import str_to_datetime
218+
219+
monday = str_to_datetime('2021-05-3 12:00:01')
220+
friday = str_to_datetime('2021-05-7 12:00:01')
221+
222+
print(to_day_of_week_int(monday), type(to_day_of_week_int(monday)))
223+
>>> 0 <class 'int'>
224+
print(to_day_of_week_int(friday), type(to_day_of_week_int(friday)))
225+
>>> 4 <class 'int'>
226+
161227
"""
162228
return dt.weekday()
163229

@@ -185,6 +251,20 @@ def working_day(
185251
if true, means that the day informed by the user is a working day.
186252
if false, means that the day is not a working day.
187253
254+
Examples
255+
--------
256+
from pymove.utils.datetime import str_to_datetime
257+
258+
independence_day = str_to_datetime('2021-09-7 12:00:01')
259+
# In Brazil this day is a holiday
260+
next_day = str_to_datetime('2021-09-8 12:00:01')
261+
# In Brazil this day is a Wednesday and isn't a holiday
262+
263+
print(working_day(independence_day, 'BR'), type(working_day(independence_day, 'BR')))
264+
>>> False <class 'bool'>
265+
print(working_day(next_day, 'BR'), type(working_day(next_day, 'BR')))
266+
>>> True <class 'bool'>
267+
188268
References
189269
----------
190270
Countries and States names available in https://pypi.org/project/holidays/
@@ -224,7 +304,7 @@ def now_str() -> Text:
224304
"2019-09-02 13:54:16"
225305
226306
"""
227-
return to_str(datetime.now())
307+
return datetime_to_str(datetime.now())
228308

229309

230310
def deltatime_str(deltatime_seconds: float) -> Text:
@@ -374,6 +454,18 @@ def elapsed_time_dt(start_time: datetime) -> int:
374454
Represents the time elapsed from the start time to the current time
375455
(when the function was called).
376456
457+
Examples
458+
--------
459+
from datetime import datetime
460+
from pymove.utils.datetime import str_to_datetime
461+
462+
start_time_1 = datetime(2020, 6, 29, 0, 0)
463+
start_time_2 = str_to_datetime('2020-06-29 12:45:59')
464+
465+
print(elapsed_time_dt(start_time_1))
466+
>>> 26411808666
467+
print(elapsed_time_dt(start_time_2))
468+
>>> 26365849667
377469
"""
378470
return diff_time(start_time, datetime.now())
379471

@@ -395,6 +487,19 @@ def diff_time(start_time: datetime, end_time: datetime) -> int:
395487
Represents the time elapsed from the start time to the current time
396488
(when the function was called).
397489
490+
Examples
491+
--------
492+
from datetime import datetime
493+
from pymove.utils.datetime import str_to_datetime
494+
495+
time_now = datetime.now()
496+
start_time_1 = datetime(2020, 6, 29, 0, 0)
497+
start_time_2 = str_to_datetime('2020-06-29 12:45:59')
498+
499+
print(diff_time(start_time_1, time_now))
500+
>>> 26411808665
501+
print(diff_time(start_time_2, time_now))
502+
>>> 26365849665
398503
"""
399504
return int((end_time - start_time).total_seconds() * 1000)
400505

@@ -481,6 +586,21 @@ def generate_time_statistics(
481586
DataFrame
482587
Statistics infomations of the pairwise local labels
483588
589+
Example
590+
-------
591+
df
592+
>>> local_label prev_local time_to_prev id
593+
0 house None NaN 1
594+
1 market house 720.0 1
595+
2 market market 5.0 1
596+
3 market market 1.0 1
597+
4 school market 844.0 1
598+
599+
generate_time_statistics(df)
600+
>>> local_label prev_local mean std min max sum count
601+
0 house market 844.0 0.000000 844.0 844.0 844.0 1
602+
1 market house 720.0 0.000000 720.0 720.0 720.0 1
603+
2 market market 3.0 2.828427 1.0 5.0 6.0 2
484604
"""
485605
df_statistics = data.groupby(
486606
[local_label, PREV_LOCAL]
@@ -514,6 +634,15 @@ def _calc_time_threshold(seg_mean: float, seg_std: float) -> float:
514634
The threshold based on the mean and standard deviation
515635
of transition time for the segment.
516636
637+
Examples
638+
--------
639+
print(_calc_time_threshold(12.3,2.1))
640+
>>> 14.4
641+
print(_calc_time_threshold(1,1.5))
642+
>>> 2.5
643+
print(_calc_time_threshold(-2,2))
644+
>>> 0.0
645+
517646
"""
518647
threshold = seg_std + seg_mean
519648
threshold = float('{:.1f}'.format(threshold))
@@ -549,6 +678,35 @@ def threshold_time_statistics(
549678
DataFrame of time statistics with the aditional feature: threshold,
550679
which indicates the time limit of the trajectory segment, or None
551680
681+
Example
682+
-------
683+
from pymove.utils.datetime import generate_time_statistics, _calc_time_threshold
684+
685+
df
686+
>>> local_label prev_local time_to_prev id
687+
0 house None NaN 1
688+
1 market house 720.0 1
689+
2 market market 5.0 1
690+
3 market market 1.0 1
691+
4 school market 844.0 1
692+
693+
statistics = generate_time_statistics(df)
694+
statistics
695+
>>> local_label prev_local mean std min max sum count
696+
0 house market 844.0 0.000000 844.0 844.0 844.0 1
697+
1 market house 720.0 0.000000 720.0 720.0 720.0 1
698+
2 market market 3.0 2.828427 1.0 5.0 6.0 2
699+
700+
threshold_time_statistics(statistics)
701+
>>> local_label prev_local mean std min max sum count
702+
0 house market 844.0 0.000000 844.0 844.0 844.0 1
703+
1 market house 720.0 0.000000 720.0 720.0 720.0 1
704+
2 market market 3.0 2.828427 1.0 5.0 6.0 2
705+
706+
threshold
707+
0 844.0
708+
1 720.0
709+
2 5.8
552710
"""
553711
if not inplace:
554712
df_statistics = df_statistics.copy()

0 commit comments

Comments
 (0)