dartdog
dartdog

Reputation: 10862

Extract Pandas index value as single date time stamp variable, Not as index

So I have a dataFrame:

            Units   fcast   currerr curpercent  fcastcum  unitscum  cumerrpercent
2013-09-01   3561    NaN    NaN  NaN     NaN     NaN     NaN
2013-10-01   3480    NaN    NaN  NaN     NaN     NaN     NaN
2013-11-01   3071    NaN    NaN  NaN     NaN     NaN     NaN
2013-12-01   3234    NaN    NaN  NaN     NaN     NaN     NaN
2014-01-01   2610    2706   -96 -3.678161 2706   2610   -3.678161
2014-02-01   NaN     3117   NaN  NaN     5823    NaN     NaN
2014-03-01   NaN     3943   NaN  NaN     9766    NaN     NaN

And I want to load a value, the index of the current month which is found by getting the last item that has "units" filled in, into a variable, "curr_month" that will have a number of uses (including text display and using as a slicing operator)

This is way ugly but almost works:

curr_month=mergederrs['Units'].dropna()
curr_month=curr_month[-1:].index
curr_month 

But curr_month is

<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01]
Length: 1, Freq: None, Timezone: None

Which is Unhashable, so this fails

mergederrs[curr_month:]

The docs are great for creating the DF but a bit sparse of getting individual items out!

Upvotes: 2

Views: 496

Answers (1)

DSM
DSM

Reputation: 353099

I'd probably write

>>> df.Units.last_valid_index()
Timestamp('2014-01-01 00:00:00')

but a slight tweak on your approach should work too:

>>> df.Units.dropna().index[-1]
Timestamp('2014-01-01 00:00:00')

It's the difference between somelist[-1:] and somelist[-1].

[Note that I'm assuming that all of the nan values come at the end. If there are valids and then NaNs and then valids, and you want the last valid in the first group, that would be slightly different.]

Upvotes: 4

Related Questions