Попытка объединить 2 фрейма данных, но сортировка дает ошибку из-за разницы в форматах даты в индексе - PullRequest
0 голосов
/ 05 августа 2020

У меня есть 2 фрейма данных df1 и df2 следующим образом:

df1.tail()
Out[104]: 
             Open    High    Low  Close  Adjusted Close     Volume
Date                                                              
2020-07-20  12.90  13.092  12.71  12.72           12.72  4676587.0
2020-07-21  12.83  13.270  12.81  13.16           13.16  5730810.0
2020-07-22  13.05  13.385  13.00  13.07           13.07  4860872.0
2020-07-23  13.02  13.170  12.62  12.71           12.71  5684962.0
2020-07-24  12.59  12.860  12.30  12.75           12.75  4173960.0

df2.tail()
Out[105]: 
                            Open   High  ...  Adjusted Close    Volume
Date                                     ...                          
2020-07-27 00:00:00+00:00  12.81  13.31  ...           13.24   4692296
2020-07-28 00:00:00+00:00  12.97  13.12  ...           12.91   3546374
2020-07-29 00:00:00+00:00  13.00  13.36  ...           13.18   4758535
2020-07-30 00:00:00+00:00  12.83  13.25  ...           13.18   6616336
2020-07-31 00:00:00+00:00  12.92  13.13  ...           13.00  10357413

[5 rows x 6 columns]

df1.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 938 entries, 2020-07-17 to 2020-07-24
Data columns (total 6 columns):
Open              938 non-null float64
High              938 non-null float64
Low               938 non-null float64
Close             938 non-null float64
Adjusted Close    938 non-null float64
Volume            938 non-null float64
dtypes: float64(6)
memory usage: 51.3 KB

df2.info()
<class 'pandas.core.frame.DataFrame'>
Index: 5 entries, 2020-07-27 00:00:00+00:00 to 2020-07-31 00:00:00+00:00
Data columns (total 6 columns):
Open              5 non-null float64
High              5 non-null float64
Low               5 non-null float64
Close             5 non-null float64
Adjusted Close    5 non-null float64
Volume            5 non-null int64
dtypes: float64(5), int64(1)
memory usage: 280.0+ bytes

Затем я объединяю df1 и df2, используя "concat".

df_merged = pd.concat( [ df1, df2], axis = 0, sort=False)

df_merged
Out[109]: 
                            Open   High  ...  Adjusted Close      Volume
Date                                     ...                            
2020-07-17 00:00:00        13.54  13.86  ...           13.06   9756100.0
2020-07-16 00:00:00        12.81  13.70  ...           13.49  16686700.0
2020-07-15 00:00:00        12.85  12.97  ...           12.69  11691100.0
2020-07-14 00:00:00        11.38  12.70  ...           12.61  11422900.0
2020-07-13 00:00:00        11.53  11.98  ...           11.51   7850600.0
                         ...    ...  ...             ...         ...
2020-07-27 00:00:00+00:00  12.81  13.31  ...           13.24   4692296.0
2020-07-28 00:00:00+00:00  12.97  13.12  ...           12.91   3546374.0
2020-07-29 00:00:00+00:00  13.00  13.36  ...           13.18   4758535.0
2020-07-30 00:00:00+00:00  12.83  13.25  ...           13.18   6616336.0
2020-07-31 00:00:00+00:00  12.92  13.13  ...           13.00  10357413.0

[943 rows x 6 columns]

Как я могу получить индекс для df2 в формате datetime без часового пояса?

ВАЖНО: Я хочу, чтобы весь индекс был похож на индекс df1, чтобы я мог сортировать по индексу без получения следующей ошибки.

df_merged.sort_index( axis=0, ascending=True, inplace=True)

TypeError: '<' not supported between instances of 'str' and 'Timestamp'
...