@@ -38,6 +38,24 @@ def reduce_mem_usage_automatic(df: DataFrame):
3838 df : dataframe
3939 The input data to which the operation will be performed.
4040
41+ Examples
42+ --------
43+ >>> from pymove.utils.mem import reduce_mem_usage_automatic
44+ >>> df
45+ lat lon datetime id
46+ 0 39.984094 116.319236 2008-10-23 05:53:05 1
47+ 1 39.984198 116.319322 2008-10-23 05:53:06 1
48+ 2 39.984224 116.319402 2008-10-23 05:53:11 1
49+ 3 39.984211 116.319389 2008-10-23 05:53:16 1
50+ 4 39.984217 116.319422 2008-10-23 05:53:21 1
51+ 5 39.984710 116.319865 2008-10-23 05:53:23 1
52+ 6 39.984674 116.319810 2008-10-23 05:53:28 1
53+ 7 39.984623 116.319773 2008-10-23 05:53:33 1
54+ 8 39.984606 116.319732 2008-10-23 05:53:38 1
55+ >>> reduce_mem_usage_automatic(df)
56+ Memory usage of dataframe is 0.00 MB
57+ Memory usage after optimization is: 0.00 MB
58+ Decreased by 26.0 %
4159 """
4260 start_mem = df .memory_usage ().sum () / 1024 ** 2
4361 logger .info ('Memory usage of dataframe is {:.2f} MB' .format (start_mem ))
@@ -138,6 +156,23 @@ def total_size(
138156 float
139157 The memory used by the given object
140158
159+ Examples
160+ --------
161+ >>> from pymove.utils.mem import total_size
162+ >>> df
163+ lat lon datetime id
164+ 0 39.984094 116.319236 2008-10-23 05:53:05 1
165+ 1 39.984198 116.319322 2008-10-23 05:53:06 1
166+ 2 39.984224 116.319402 2008-10-23 05:53:11 1
167+ 3 39.984211 116.319389 2008-10-23 05:53:16 1
168+ 4 39.984217 116.319422 2008-10-23 05:53:21 1
169+ 5 39.984710 116.319865 2008-10-23 05:53:23 1
170+ 6 39.984674 116.319810 2008-10-23 05:53:28 1
171+ 7 39.984623 116.319773 2008-10-23 05:53:33 1
172+ 8 39.984606 116.319732 2008-10-23 05:53:38 1
173+ >>> total_size(df)
174+ Size in bytes: 432, Type: <class 'pandas.core.frame.DataFrame'>
175+ 432
141176 """
142177 if handlers is None :
143178 handlers = {}
@@ -194,7 +229,19 @@ def begin_operation(name: Text) -> Dict:
194229 -------
195230 dict
196231 dictionary with the operation stats
197-
232+ Examples
233+ --------
234+ >>> from pymove.utils.mem import begin_operation
235+ >>> print(begin_operation('move_data'), type(begin_operation('move_data')))
236+ {'process': psutil.Process(pid=103401, name='python',
237+ status='running', started='21:48:11'),
238+ 'init': 293732352, 'start': 1622082973.8825781, 'name': 'move_data'}
239+ <class 'dict'>
240+ >>> print(begin_operation('mdf'), type(begin_operation('mdf')))
241+ {'process': psutil.Process(pid=103401, name='python',
242+ status='running', started='21:48:11'),
243+ 'init': 293732352, 'start': 1622082973.8850513, 'name': 'mdf'}
244+ <class 'dict'>
198245 """
199246 process = psutil .Process (os .getpid ())
200247 init = process .memory_info ()[0 ]
@@ -216,6 +263,17 @@ def end_operation(operation: Dict) -> Dict:
216263 dict
217264 dictionary with the operation execution stats
218265
266+ Examples
267+ --------
268+ >>> from pymove.utils.mem import end_operation
269+ >>> stats = {'process': psutil.Process(pid=103401, name='python',
270+ status='running', started='21:48:11'),
271+ 'init': 293732352,
272+ 'start': 1622083075.4811873,
273+ 'name': 'move_data'}
274+ >>> print(end_operation(stats), type(end_operation(stats)))
275+ {'name': 'move_data', 'time in seconds': 0.0014350414276123047,
276+ 'memory': '0.0 B'} <class 'dict'>
219277 """
220278 finish = operation ['process' ].memory_info ()[0 ]
221279 last_operation_name = operation ['name' ]
@@ -244,7 +302,11 @@ def sizeof_fmt(mem_usage: int, suffix: Optional[Text] = 'B') -> Text:
244302 -------
245303 str
246304 A string of the memory usage in a more readable format
247-
305+ Examples
306+ --------
307+ >>> from pymove.utils.mem import sizeof_fmt
308+ >>> print(sizeof_fmt(6.64,'MB'), type(sizeof_fmt(6.64,'MB')))
309+ 6.6 MB <class 'str'>
248310 """
249311 for unit in ['' , 'Ki' , 'Mi' , 'Gi' , 'Ti' , 'Pi' , 'Ei' , 'Zi' ]:
250312 if abs (mem_usage ) < 1024.0 :
@@ -272,7 +334,22 @@ def top_mem_vars(
272334 -------
273335 DataFrame
274336 dataframe with variables names and sizes
275-
337+ Examples
338+ --------
339+ >>> from pymove.utils.mem import top_mem_vars
340+ >>> print(top_mem_vars(globals()), type(top_mem_vars(globals())))
341+ var mem
342+ 0 Out 1.1 KiB
343+ 1 In 776.0 B
344+ 2 df2 432.0 B
345+ 3 df 304.0 B
346+ 4 stats 232.0 B
347+ 5 reduce_mem_usage_automatic 136.0 B
348+ 6 total_size 136.0 B
349+ 7 begin_operation 136.0 B
350+ 8 end_operation 136.0 B
351+ 9 sizeof_fmt 136.0 B
352+ <class 'pandas.core.frame.DataFrame'>
276353 """
277354 if variables is None :
278355 variables = globals ()
0 commit comments