soft-matter / trackpy

Python particle tracking toolkit
http://soft-matter.github.io/trackpy
Other
443 stars 131 forks source link

Update motion.py to fix TypeError: mean() got an unexpected keyword argument 'level' on emsd function #740

Closed marcocaggioni closed 9 months ago

marcocaggioni commented 1 year ago

Running the walkthrough with pandas 1.5.3 I was getting this futurewarning when calculating the average mean square displacement with emsd function in the motion.py module

/usr/local/lib/python3.10/dist-packages/trackpy/motion.py:235: FutureWarning: Using the level keyword in DataFrame and Series aggregations is deprecated and will be removed in a future version. Use groupby instead. df.median(level=1) should use df.groupby(level=1).median().
  results = msds.mul(msds['N'], axis=0).mean(level=1)  # weighted average
/usr/local/lib/python3.10/dist-packages/trackpy/motion.py:236: FutureWarning: Using the level keyword in DataFrame and Series aggregations is deprecated and will be removed in a future version. Use groupby instead. df.median(level=1) should use df.groupby(level=1).median().
  results = results.div(msds['N'].mean(level=1), axis=0)  # weights normalized

Running with pandas 2.0.3 I get this error:

image

After modification I can reproduce the same results as in the walkthrough

image

Sending back in case you want to include, I tested this modified version works fine back to pandas 1.5.3 Thanks for this great library!