Loss of values ​​when building a series from a DataFrame column

I have a DataFrame tdconsisting of the following columns:

In [111]: td.head(5)
Out[111]:
         Date      Time    Price
0  2015-09-21  00:01:26  4303.00
1  2015-09-21  00:01:33  4303.00
2  2015-09-21  00:02:21  4303.50
3  2015-09-21  00:02:21  4303.50
4  2015-09-21  00:02:31  4303.25

My goal is to have a series with Datetime and Price.

I tried:

s = pd.Series(td['Price'], index=pd.to_datetime(td['Date'] + ' ' + td['Time']))

But we get the result:

>>> s
2015-09-21 00:01:26   NaN
2015-09-21 00:01:33   NaN
2015-09-21 00:02:21   NaN
2015-09-21 00:02:21   NaN
                       ..
2015-09-25 16:59:58   NaN
2015-09-25 16:59:58   NaN
2015-09-25 16:59:58   NaN
2015-09-25 16:59:59   NaN
Name: Price, dtype: float64

All values ​​from "Prices" are NaN. Any hint on what I'm doing wrong?

+4
source share
1 answer

When you create a series from a DataFrame column and pass to the index, the column will be reindexed according to the new index.

Datetime td['Price'], (NaN).

- td['Price'].values :

>>> pd.Series(td['Price'].values, index=pd.to_datetime(td['Date']+' '+td['Time'])
2015-09-21 00:01:26    4303.00
2015-09-21 00:01:33    4303.00
2015-09-21 00:02:21    4303.50
2015-09-21 00:02:21    4303.50
2015-09-21 00:02:31    4303.25
...

td['Price'].values , NumPy: , pandas .

+2

Source: https://habr.com/ru/post/1609215/


All Articles