@@ -36,7 +36,7 @@ def read_csv(
3636 type_ : Optional [Text ] = TYPE_PANDAS ,
3737 n_partitions : Optional [int ] = 1 ,
3838 ** kwargs
39- ):
39+ ) -> MoveDataFrame :
4040 """
4141 Reads a `csv` file and structures the data.
4242
@@ -70,6 +70,19 @@ def read_csv(
7070 MoveDataFrameAbstract subclass
7171 Trajectory data
7272
73+ Examples
74+ --------
75+ >>> from pymove.utils.trajectories import read_csv
76+ >>> move_df = read_csv('geolife_sample.csv')
77+ >>> move_df.head()
78+ lat lon datetime id
79+ 0 39.984094 116.319236 2008-10-23 05:53:05 1
80+ 1 39.984198 116.319322 2008-10-23 05:53:06 1
81+ 2 39.984224 116.319402 2008-10-23 05:53:11 1
82+ 3 39.984211 116.319389 2008-10-23 05:53:16 1
83+ 4 39.984217 116.319422 2008-10-23 05:53:21 1
84+ >>> type(move_df)
85+ <class 'pymove.core.pandas.PandasMoveDataFrame'>
7386 """
7487 data = _read_csv (
7588 filepath_or_buffer ,
@@ -95,6 +108,12 @@ def invert_dict(d: Dict) -> Dict:
95108 dict
96109 inverted dict
97110
111+ Examples
112+ --------
113+ >>> from pymove.utils.trajectories import invert_dict
114+ >>> traj_dict = {'a': 1, 'b': 2}
115+ >>> invert_dict(traj_dict)
116+ {1: 'a, 2: 'b'}
98117 """
99118 return {v : k for k , v in d .items ()}
100119
@@ -125,10 +144,10 @@ def flatten_dict(
125144
126145 Examples
127146 --------
128- >>> d = { 'a': 1, 'b': { 'c': 2, 'd': 3}}
147+ >>> from pymove.utils.trajectories import flatten_dict
148+ >>> d = {'a': 1, 'b': {'c': 2, 'd': 3}}
129149 >>> flatten_dict(d)
130- { 'a': 1, 'b_c': 2, 'b_d': 3 }
131-
150+ {'a': 1, 'b_c': 2, 'b_d': 3}
132151 """
133152 if not isinstance (d , dict ):
134153 return {parent_key : d }
@@ -164,12 +183,27 @@ def flatten_columns(data: DataFrame, columns: List) -> DataFrame:
164183
165184 Examples
166185 --------
167- >>> d = {'a': 1, 'b': {'c': 2, 'd': 3}}
168- >>>> data = pd.DataFrame({'col1': [1], 'col2': [d]})
169- >>>> flatten_columns(data, ['col2'])
170- col1 col2_b_d col2_a col2_b_c
171- 0 1 3 1 2
172-
186+ >>> from pymove.utils.trajectories import flatten_columns
187+ >>> move_df
188+ lat lon datetime id dict_column
189+ 0 39.984094 116.319236 2008-10-23 05:53:05 1 {'a': 1}
190+ 1 39.984198 116.319322 2008-10-23 05:53:06 1 {'b': 2}
191+ 2 39.984224 116.319402 2008-10-23 05:53:11 1 {'c': 3, 'a': 4}
192+ 3 39.984211 116.319389 2008-10-23 05:53:16 1 {'b': 2}
193+ 4 39.984217 116.319422 2008-10-23 05:53:21 1 {'a': 3, 'c': 2}
194+ >>> flatten_columns(move_df, columns='dict_column')
195+ lat lon datetime id \
196+ dict_column_b dict_column_c dict_column_a
197+ 0 39.984094 116.319236 2008-10-23 05:53:05 1 \
198+ NaN NaN 1.0
199+ 1 39.984198 116.319322 2008-10-23 05:53:06 1 \
200+ 2.0 NaN NaN
201+ 2 39.984224 116.319402 2008-10-23 05:53:11 1 \
202+ NaN 3.0 4.0
203+ 3 39.984211 116.319389 2008-10-23 05:53:16 1 \
204+ 2.0 NaN NaN
205+ 4 39.984217 116.319422 2008-10-23 05:53:21 1 \
206+ NaN 2.0 3.0
173207 """
174208 data = data .copy ()
175209 if not isinstance (columns , list ):
@@ -218,6 +252,16 @@ def shift(
218252 ----------
219253 https://stackoverflow.com/questions/30399534/shift-elements-in-a-numpy-array
220254
255+ Examples
256+ --------
257+ >>> from pymove.utils.trajectories import shift
258+ >>> array = [1, 2, 3, 4, 5, 6, 7]
259+ >>> shift(array, 1)
260+ [0 1 2 3 4 5 6]
261+ >>> shift(array, 0)
262+ [1, 2, 3, 4, 5, 6, 7]
263+ >>> shift(array, -1)
264+ [2 3 4 5 6 7 0]
221265 """
222266 result = np .empty_like (arr )
223267 if fill_value is None :
@@ -254,6 +298,13 @@ def fill_list_with_new_values(original_list: List, new_list_values: List):
254298 new_list_values : list.
255299 The list from which elements will be copied
256300
301+ Example
302+ -------
303+ >>> from pymove.utils.trajectories import fill_list_with_new_values
304+ >>> lst = [1, 2, 3, 4]
305+ >>> fill_list_with_new_values(lt, ['a','b'])
306+ >>> print(lst)
307+ ['a', 'b', 3, 4]
257308 """
258309 n = len (new_list_values )
259310 original_list [:n ] = new_list_values
@@ -272,6 +323,13 @@ def object_for_array(object_: Text) -> ndarray:
272323 -------
273324 array
274325 object converted to a list
326+
327+ Examples
328+ --------
329+ >>> from pymove.utils.trajectories import object_for_array
330+ >>> list_str = '[1,2,3,4,5]'
331+ >>> object_for_array(list_str)
332+ array([1., 2., 3., 4., 5.], dtype=float32)
275333 """
276334 if object_ is None :
277335 return object_
@@ -284,7 +342,7 @@ def object_for_array(object_: Text) -> ndarray:
284342 return conv .astype ('object_' )
285343
286344
287- def column_to_array (data : DataFrame , column : Text ):
345+ def column_to_array (data : DataFrame , column : Text ) -> DataFrame :
288346 """
289347 Transforms all columns values to list.
290348
@@ -295,6 +353,29 @@ def column_to_array(data: DataFrame, column: Text):
295353
296354 column : str
297355 Label of data referring to the column for conversion
356+
357+ Returns
358+ -------
359+ dataframe
360+ Dataframe with the selected column converted to list
361+
362+ Example
363+ -------
364+ >>> from pymove.utils.trajectories import column_to_array
365+ >>> move_df
366+ lat lon datetime id list_column
367+ 0 39.984094 116.319236 2008-10-23 05:53:05 1 '[1,2]'
368+ 1 39.984198 116.319322 2008-10-23 05:53:06 1 '[3,4]'
369+ 2 39.984224 116.319402 2008-10-23 05:53:11 1 '[5,6]'
370+ 3 39.984211 116.319389 2008-10-23 05:53:16 1 '[7,8]'
371+ 4 39.984217 116.319422 2008-10-23 05:53:21 1 '[9,10]'
372+ >>> column_to_array(move_df, column='list_column')
373+ lat lon datetime id list_column
374+ 0 39.984094 116.319236 2008-10-23 05:53:05 1 [1.0,2.0]
375+ 1 39.984198 116.319322 2008-10-23 05:53:06 1 [3.0,4.0]
376+ 2 39.984224 116.319402 2008-10-23 05:53:11 1 [5.0,6.0]
377+ 3 39.984211 116.319389 2008-10-23 05:53:16 1 [7.0,8.0]
378+ 4 39.984217 116.319422 2008-10-23 05:53:21 1 [9.0,10.0]
298379 """
299380 data = data .copy ()
300381 if column not in data :
0 commit comments