Home > Back-end >  Pandas How to retrieve the value of datetime index with apply lambda?
Pandas How to retrieve the value of datetime index with apply lambda?

Time:03-18

Here is the Demo:

import pandas as pd
from datetime import datetime

df_test2 = pd.DataFrame({'dt_str': {0: '03/01/2017-09:16:00',
  1: '03/01/2017-09:17:00',
  2: '03/01/2017-09:18:00',
  3: '03/01/2017-09:19:00',
  4: '03/01/2017-09:20:00'},
 'Open': {0: 21920, 1: 21945, 2: 21940, 3: 21981, 4: 21988},
 'Close': {0: 21945, 1: 21940, 2: 21981, 3: 21988, 4: 21977},
 'Volume': {0: 935, 1: 541, 2: 651, 3: 314, 4: 318}})

df_test2['dt'] = df_test2['dt_str'].apply(lambda x: datetime.strptime(x, '%d/%m/%Y-%H:%M:%S'))

df_test2.reset_index(drop=True, inplace=True)
df_test2.set_index('dt', inplace=True)


# The question is how can I get the value of the datetime index in lambda function

def condition(x):
    # handling index value, like checking the datetime's weekday
    print(x['dt'].weekday())
    return True


df_test2.apply(lambda x: condition(x), axis=1)

When I'm calling x['dt'] it raised Key error:

KeyError                                  Traceback (most recent call last)

xxx
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'dt'

Update

if I use x.index, it raised:

AttributeError: 'Index' object has no attribute 'weekday'

CodePudding user response:

Strangely enough, you need to use x.name instead of x['dt'], since dt is now the index:

def condition(x):
    # handling index value, like checking the datetime's weekday
    print(x.name.weekday())
    return True

df_test2.apply(lambda x: condition(x), axis=1)

(Also, note that when your function takes one param, it's redundant to use .apply(lambda x: condition(x), axis=1) - instead just write .apply(condition, axis=1))

Output:

In [289]: def condition(x):
     ...:     # handling index value, like checking the datetime's weekday
     ...:     print(x.name.weekday())
     ...:     return True
     ...: 
     ...: df_test2.apply(lambda x: condition(x), axis=1)
1
1
1
1
1
Out[289]: 
dt
2017-01-03 09:16:00    True
2017-01-03 09:17:00    True
2017-01-03 09:18:00    True
2017-01-03 09:19:00    True
2017-01-03 09:20:00    True
dtype: bool
  • Related