2

I want to retrieve the date of one index position of a Pandas data frame and paste it into the LineEdit of a PyQt Application.

What I have so far is:

    purchase = sales [['Total','Date']]
    pandas_value = purchase.iloc[-1:]['Date'] # last position of the "Date" column
    pyqt_value = str(pandas_value)

    # This returns : 

    67   2016-10-20
    Name: Data, dtype: datetime64[ns]

The entire output appears in the LineEdit as : 67 2016-10-20 Name: Data, dtype: datetime64[ns]

I have also tried converting the date, to no avail:

    pandas_value.strftime('%Y-%m-%d')

    'Series' object has no attribute 'strftime'

Is there a way to retrieve and paste just the date like : 2016-10-20 ?

Or better : Is there a way to retrieve any value as a string from any index position in pandas?

Thanks in advance for any help.

rainer
  • 3,295
  • 5
  • 34
  • 50

2 Answers2

2

You can do it this way:

In [37]: df
Out[37]:
        Date         a
0 2016-01-01  0.228208
1 2016-01-02  0.695593
2 2016-01-03  0.493608
3 2016-01-04  0.728678
4 2016-01-05  0.369823
5 2016-01-06  0.336615
6 2016-01-07  0.012200
7 2016-01-08  0.481646
8 2016-01-09  0.773467
9 2016-01-10  0.550114

In [38]: df.iloc[-1, df.columns.get_loc('Date')].strftime('%Y-%m-%d')
Out[38]: '2016-01-10'
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • Working perfectly ...many thanks ... I'm still learning Pandas and slowly getting the hang of it. It's great fun .. – rainer Oct 23 '16 at 11:57
1

pandas returns it as Series which is like a list (normally it keeps one row or one column of data) so you have to use index to get value. You Series has only one value so you can use index [0] (or maybe [67] because your text shows value 67 as index)

 pyqt_values = str(panda_values[0])
furas
  • 134,197
  • 12
  • 106
  • 148
  • Furas, thanks for your suggestion. Using [0] returns : KeyError: 0. Using [67] gets me: argument 1 has unexpected type 'Timestamp'. I also need [-1], because I need the value of the last row. – rainer Oct 23 '16 at 11:46
  • I have played around with the suggestion you have posted the other day, and it would actually work only after converting the value to monetary. I still don't fully understand Pandas and just send this as feedback. – rainer Oct 23 '16 at 11:55