If I create a non-empty empty DataFrame, then I can access the index while using apply by accessing the name field.
import pandas as pd
pd.__version__ # '1.2.4'
df = pd.DataFrame({'val': [5,2,1]}, index=[1,2,3])
print(df.apply(lambda x: x.name, axis=1))
1 1
2 2
3 3
dtype: int64
However, this does not seem to be the case for an empty DataFrame.
df = pd.DataFrame(index=[1,2,3])
print(df.apply(lambda x: x.name, axis=1))
1 NaN
2 NaN
3 NaN
dtype: float64
Why is this the case and what can be done to achieve the non-empty behaviour?
Further context
In my case, I am actually interested in a behaviour similar to this:
def f(n):
return [n, n+1]
df.apply(lambda x: f(x.name), axis=1, result_type='expand')
I would like to be able to join the result to the DataFrame afterwards.