简体   繁体   English

将单个 Pandas 列转换为 DateTime

[英]Convert a single Pandas column to DateTime

In my dataframe, I set the index of each column to 'Time' and then did frame = frame.astype(float) to convert all the the other data to floats.在我的 dataframe 中,我将每列的索引设置为“时间”,然后frame = frame.astype(float)将所有其他数据转换为浮点数。 However, I now need the default indices (0, 1, 2, etc) but I still want to set the 'Time' column to a date time format.但是,我现在需要默认索引(0、1、2 等),但我仍然想将“时间”列设置为日期时间格式。 I've tried a few different ways of doing this, they either work but mess up the time (says its 1970 instead of 2021) or they result in TypeError: Cannot cast DatetimeArray to dtype float64我尝试了几种不同的方法,它们要么工作,但弄乱了时间(说它是 1970 年而不是 2021 年),或者它们导致TypeError: Cannot cast DatetimeArray to dtype float64

This is similar to the dataframe I want (but with the times messed up):这类似于我想要的 dataframe (但时间搞砸了):

                          Time      Open      High       Low     Close
0   1970-01-01 00:27:18.185760  57141.92  57157.16  57141.92  57147.00
1   1970-01-01 00:27:18.185820  57145.48  57149.15  57124.62  57139.75
2   1970-01-01 00:27:18.185880  57126.75  57173.11  57126.74  57142.20
3   1970-01-01 00:27:18.185940  57163.42  57163.42  57079.10  57135.31
4   1970-01-01 00:27:18.186000  57084.42  57110.00  57084.42  57092.95

I've tried changing the format of the 'Time' column with:我尝试使用以下方法更改“时间”列的格式:

frame['Time'] = pd.to_datetime(frame['Time'])

And

frame['Time'] = frame['Time'].apply(pd.to_datetime)

And I have also tried changing the types of the other columns in a similar way我也尝试过以类似的方式更改其他列的类型

frame[['Open','High','Low','Close']] = frame[['Open','High','Low','Close']].apply(frame.astype(float))

And I tried this before and after applying pd.to_datetime我在应用pd.to_datetime之前和之后都试过了


EDIT编辑

Going to give some more information because I haven't been specific enough.将提供更多信息,因为我还不够具体。 The code below retrieves data from an API and puts it into a DataFrame.下面的代码从 API 检索数据并将其放入 DataFrame。 The response from the API is a list of lists, with each sublist containing 10 elements (I think, can't remember now). API 的响应是一个列表列表,每个子列表包含 10 个元素(我想,现在不记得了)。 I only want the data up to 'Close'.我只希望数据达到“关闭”。

def get_historical_futures_data(symbol, interval, lookback):
    frame = pd.DataFrame(client.futures_historical_klines(symbol, interval, lookback+' min ago UTC'))
    frame = frame.iloc[:,:5]
    frame.columns = ['Time','Open','High','Low','Close']
    frame = frame.set_index('Time')
    frame.index = pd.to_datetime(frame.index, unit='ms')
    frame = frame.astype(float)
    print(frame)
    frames.append(frame)
                         Open      High       Low     Close
Time                                                       
2021-11-29 14:27:00  57220.49  57220.50  57185.95  57190.01
2021-11-29 14:28:00  57190.00  57209.21  57161.74  57177.28
2021-11-29 14:29:00  57177.28  57182.61  57160.26  57164.46
2021-11-29 14:30:00  57164.46  57186.99  57154.32  57155.99
2021-11-29 14:31:00  57156.00  57179.74  57154.33  57179.74

Above is the code (and its output), I had previously, however, in another part of my code, I have realised that it is much easier for me to keep the row index numbers, so I do not want to make 'Time' the index of each row.以上是我之前的代码(及其输出),但是,在我的代码的另一部分,我意识到保留行索引号对我来说更容易,所以我不想让“时间”每行的索引。 Instead, I want the index of each row to remain, and then the rest of the data frame to come after, similar to this:相反,我希望保留每一行的索引,然后将数据帧的 rest 放在后面,类似于这样:

                          Time      Open      High       Low     Close
0   1970-01-01 00:27:18.185760  57141.92  57157.16  57141.92  57147.00
1   1970-01-01 00:27:18.185820  57145.48  57149.15  57124.62  57139.75
2   1970-01-01 00:27:18.185880  57126.75  57173.11  57126.74  57142.20
3   1970-01-01 00:27:18.185940  57163.42  57163.42  57079.10  57135.31
4   1970-01-01 00:27:18.186000  57084.42  57110.00  57084.42  57092.95

My issue is, that I am unable to make the 'Time' column into a DateTime type as well as make the other columns (Open, High, Low, Close) into float type.我的问题是,我无法将“时间”列设为 DateTime 类型,也无法将其他列(Open、High、Low、Close)设为浮点类型。 I either get errors about type casting, or the Time column gets messed up and says 1970 instead of 2021.我要么收到有关类型转换的错误,要么时间列搞砸了,说 1970 而不是 2021。

How do I make every column (EXCEPT FOR TIME) float type, and make the Time column DateTime type?如何使每一列(时间除外)浮点类型,并使时间列 DateTime 类型?

I believe this issue might be happening because the format is not easy to find by pandas.我相信这个问题可能会发生,因为 pandas 不容易找到该格式。 Perhaps you can try using infer_datetime_format=True to enhance the formats being detected.也许您可以尝试使用infer_datetime_format=True来增强检测到的格式。

Kindly try:请尝试:

frame['Time'] = pd.to_datetime(frame['Time'],infer_datetime_format=True)

This outputs这输出

                        Time
0 1970-01-01 00:27:18.185760
1 1970-01-01 00:27:18.185820
2 1970-01-01 00:27:18.185880

And by using df.info() we can check it's an actual datetime format:通过使用df.info()我们可以检查它是一个实际的日期时间格式:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 1 columns):
 #   Column  Non-Null Count  Dtype         
---  ------  --------------  -----         
 0   Time    3 non-null      datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 152.0 bytes
None

This is the sample data used for this example:这是用于此示例的示例数据:

df = pd.DataFrame({'Time':['1970-01-01 00:27:18.185760',
                           '1970-01-01 00:27:18.185820',
                           '1970-01-01 00:27:18.185880']})

So I figured out what I was doing wrong.所以我弄清楚我做错了什么。 My times were being changed from the year 2021 to 1970 because I wasn't specifying that the unit was in milliseconds.我的时间从 2021 年更改为 1970 年,因为我没有指定单位为毫秒。 My code is very similar to what I had initially, and the solution is actually really simple:我的代码与我最初的代码非常相似,解决方案实际上非常简单:

def get_historical_futures_data(symbol, interval, lookback):
    frame = pd.DataFrame(client.futures_historical_klines(symbol, interval, lookback+' min ago UTC'))
    frame = frame.iloc[:,:5]
    frame.columns = ['Time','Open','High','Low','Close']
    frame['Time'] = pd.to_datetime(frame['Time'], unit='ms')
    print(frame)
    frames.append(frame)

Output is: Output 是:

                  Time      Open      High       Low     Close
0   2021-11-29 19:17:00  58388.41  58401.33  58357.30  58359.75
1   2021-11-29 19:18:00  58359.74  58365.33  58270.00  58290.95
2   2021-11-29 19:19:00  58290.95  58291.80  58173.28  58188.67
3   2021-11-29 19:20:00  58188.68  58317.02  58174.30  58308.70
4   2021-11-29 19:21:00  58309.32  58365.75  58309.31  58330.55

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM