1

I'm trying to find the median of a list of timeDelta objects generated from a PANDAS dataframe. I've tried using the statistics library as such:

newList= list(DF.sort_values(['TimeDelta'])['TimeDelta'])
TDmedian = (st.median(newList))

st is what I imported the statistics library as.

But I get the error:

`TypeError: unsupported operand type(s) for /: 'str' and 'int'`

I tried to make a function to calculate it: `

def date_median(date_list):
    length = len(date_list)
    print(length)
//Checks if the length is odd cause median in odd numbered lists is the middle value
    if length % 2 != 0:
        return date_list[length//2]
    else:
//If it's even, it'll take the middle value and the one above it and generate the mean
        print((length//2), (length//2+1))
        lower = date_list[length//2]
        upper = date_list[(length//2) +1]
        return (lower + upper)/2`

And I use it like this:

`TAmedian = date_median(newList)`

And I get this error:

`TypeError: unsupported operand type(s) for /: 'str' and 'int'`

Is there an easier way to do this and if not then what I am doing wrong?

Sample Data:

DF['TimeDelta'] = [0 days 00:00:36.35700000,0 days 00:47:11.213000000]
FallingInForward
  • 285
  • 2
  • 4
  • 12

2 Answers2

0

OK. It should work. Famous last words right?

I suspect you have some element in that column of your data frame that is not numeric. It should work similar to this:

In [17]: import pandas as pd                                                                                    

In [18]: tds = [timedelta(t) for t in range(5)]                                                                 

In [19]: x = list(range(5))                                                                                     

In [20]: df = pd.DataFrame({'x': x, 'time delta': tds})                                                         

In [21]: df                                                                                                     
Out[21]: 
   x time delta
0  0     0 days
1  1     1 days
2  2     2 days
3  3     3 days
4  4     4 days

In [22]: import numpy as np                                                                                     

In [23]: np.median(df['time delta'])                                                                            
Out[23]: numpy.timedelta64(172800000000000,'ns')

So, have you tested the data frame to see if there are some non-numeric values in the column? Easiest is just with the info() command. It should look similar to this. If it shows "Object", you need to find out why.

In [24]: df.info()                                                                                              
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
 #   Column      Non-Null Count  Dtype          
---  ------      --------------  -----          
 0   x           5 non-null      int64          
 1   time delta  5 non-null      timedelta64[ns]
dtypes: int64(1), timedelta64[ns](1)
memory usage: 208.0 bytes

In [25]: df.describe()                                                                                          
Out[25]: 
              x              time delta
count  5.000000                       5
mean   2.000000         2 days 00:00:00
std    1.581139  1 days 13:56:50.394919
min    0.000000         0 days 00:00:00
25%    1.000000         1 days 00:00:00
50%    2.000000         2 days 00:00:00
75%    3.000000         3 days 00:00:00
max    4.000000         4 days 00:00:00

this is a good post about hunting for non-numeric values:

Finding non-numeric rows in dataframe in pandas?

AirSquid
  • 10,214
  • 2
  • 7
  • 31
0

Why convert to a list? pandas.DataFrame has in store everything you need:

import pandas as pd

DF = pd.DataFrame({'TimeDelta': pd.to_timedelta(['0 days 00:00:36.35700000', 
                                                 '0 days 00:47:11.213000000'])})

DF['TimeDelta'].mean()
# Timedelta('0 days 00:23:53.785000')
DF['TimeDelta'].median()
# Timedelta('0 days 00:23:53.785000')

Of course, if you don't have a df in the first place, you could also do without, like e.g.

pd.to_timedelta(['0 days 00:00:36.35700000', '0 days 00:47:11.213000000']).median()
FObersteiner
  • 22,500
  • 8
  • 42
  • 72