Python ValueError: Both dates must have the same UTC offset with .loc
Python ValueError: Both dates must have the same UTC offset with .loc
我在使用 loc 获取两个时间段之间 pandas DataFrame 中的所有条目时遇到问题。比如下面两行都给了我一个值:
periods.loc[start]
periods.loc[end]
但是,当我 运行 以下内容时,我得到一个 ValueError: "Both dates must have the same UTC offset":
periods.loc[start:end]
我期望得到一个 DataFrame,其中包含这两个日期之间的所有条目。所有条目都是 "yyyy-mm-dd" 形式的字符串。
以下是句点的前 10 个条目:
0 2007-01-25 09:10:02
1 2007-01-26 07:03:01
2 2007-02-02 04:50:51
3 2007-02-06 07:54:35
4 2007-02-07 06:31:05
5 2007-02-07 09:09:47
6 2007-02-07 09:43:12
7 2007-02-09 07:34:55
8 2007-02-13 04:32:04
9 2007-02-15 06:30:51
您需要将数据转换为 datetime.datetime
对象。这是一个完整的示例,说明如何执行此操作:
>>> import pandas as pd
>>> from datetime import datetime
>>> df = pd.DataFrame({"date": ["2007-01-25 09:10:02", "2007-01-26 07:03:01",
... "2007-02-02 04:50:51", "2007-02-06 07:54:35",
... "2007-02-07 06:31:05", "2007-02-07 09:09:47",
... "2007-02-07 09:43:12", "2007-02-09 07:34:55",
... "2007-02-13 04:32:04", "2007-02-15 06:30:51"]})
>>> # convert the date column to datetime object
>>> df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d %H:%M:%S")
>>> df
date
0 2007-01-25 09:10:02
1 2007-01-26 07:03:01
2 2007-02-02 04:50:51
3 2007-02-06 07:54:35
4 2007-02-07 06:31:05
5 2007-02-07 09:09:47
6 2007-02-07 09:43:12
7 2007-02-09 07:34:55
8 2007-02-13 04:32:04
9 2007-02-15 06:30:51
现在,让我们使用 start
和 end
来分割它,它们都是日期:
>>> start = "2007-01-25 09:10:02"
>>> end = "2007-02-07 08:53:51"
>>> # convert start and end from string to datetime object
>>> start = datetime.strptime(start, '%Y-%m-%d %H:%M:%S')
>>> end = datetime.strptime(end, '%Y-%m-%d %H:%M:%S')
>>> # let's access some values
>>> df.loc[(df["date"] >= start) & (df["date"] < end)] #mimics the slice object
date
0 2007-01-25 09:10:02
1 2007-01-26 07:03:01
2 2007-02-02 04:50:51
3 2007-02-06 07:54:35
4 2007-02-07 06:31:05
此外,您可以使用 loc
:
访问任何特定日期
>>> new_date = "2007-02-07 06:31:05"
>>> new_date = datetime.strptime(new_date, '%Y-%m-%d %H:%M:%S')
>>> df.loc[df["date"] == new_date]
date
4 2007-02-07 06:31:05
我在使用 loc 获取两个时间段之间 pandas DataFrame 中的所有条目时遇到问题。比如下面两行都给了我一个值:
periods.loc[start]
periods.loc[end]
但是,当我 运行 以下内容时,我得到一个 ValueError: "Both dates must have the same UTC offset":
periods.loc[start:end]
我期望得到一个 DataFrame,其中包含这两个日期之间的所有条目。所有条目都是 "yyyy-mm-dd" 形式的字符串。 以下是句点的前 10 个条目:
0 2007-01-25 09:10:02
1 2007-01-26 07:03:01
2 2007-02-02 04:50:51
3 2007-02-06 07:54:35
4 2007-02-07 06:31:05
5 2007-02-07 09:09:47
6 2007-02-07 09:43:12
7 2007-02-09 07:34:55
8 2007-02-13 04:32:04
9 2007-02-15 06:30:51
您需要将数据转换为 datetime.datetime
对象。这是一个完整的示例,说明如何执行此操作:
>>> import pandas as pd
>>> from datetime import datetime
>>> df = pd.DataFrame({"date": ["2007-01-25 09:10:02", "2007-01-26 07:03:01",
... "2007-02-02 04:50:51", "2007-02-06 07:54:35",
... "2007-02-07 06:31:05", "2007-02-07 09:09:47",
... "2007-02-07 09:43:12", "2007-02-09 07:34:55",
... "2007-02-13 04:32:04", "2007-02-15 06:30:51"]})
>>> # convert the date column to datetime object
>>> df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d %H:%M:%S")
>>> df
date
0 2007-01-25 09:10:02
1 2007-01-26 07:03:01
2 2007-02-02 04:50:51
3 2007-02-06 07:54:35
4 2007-02-07 06:31:05
5 2007-02-07 09:09:47
6 2007-02-07 09:43:12
7 2007-02-09 07:34:55
8 2007-02-13 04:32:04
9 2007-02-15 06:30:51
现在,让我们使用 start
和 end
来分割它,它们都是日期:
>>> start = "2007-01-25 09:10:02"
>>> end = "2007-02-07 08:53:51"
>>> # convert start and end from string to datetime object
>>> start = datetime.strptime(start, '%Y-%m-%d %H:%M:%S')
>>> end = datetime.strptime(end, '%Y-%m-%d %H:%M:%S')
>>> # let's access some values
>>> df.loc[(df["date"] >= start) & (df["date"] < end)] #mimics the slice object
date
0 2007-01-25 09:10:02
1 2007-01-26 07:03:01
2 2007-02-02 04:50:51
3 2007-02-06 07:54:35
4 2007-02-07 06:31:05
此外,您可以使用 loc
:
>>> new_date = "2007-02-07 06:31:05"
>>> new_date = datetime.strptime(new_date, '%Y-%m-%d %H:%M:%S')
>>> df.loc[df["date"] == new_date]
date
4 2007-02-07 06:31:05