drop_duplicates()来自极地的熊猫相当于什么?
import polars as pl\ndf = pl.DataFrame({"a":[1,1,2], "b":[2,2,3], "c":[1,2,3]})\ndf\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nshape: (3, 3)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 a \xe2\x94\x86 b \xe2\x94\x86 c \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 i64 \xe2\x94\x86 i64 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 1 \xe2\x94\x86 2 \xe2\x94\x86 1 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 1 \xe2\x94\x86 2 \xe2\x94\x86 2 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xbc\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 2 \xe2\x94\x86 3 \xe2\x94\x86 3 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n代码:
\ndf.drop_duplicates(["a", "b"])\nRun Code Online (Sandbox Code Playgroud)\n产生以下错误:
\n属性错误:未找到 drop_duplicates
\n最近升级到Python 3.8,并已安装jupyter。但是,当尝试运行时jupyter notebook出现以下错误:
File "c:\users\user\appdata\local\programs\python\python38\lib\site-packages\tornado\platform\asyncio.py", line 99, in add_handler
self.asyncio_loop.add_reader(fd, self._handle_events, fd, IOLoop.READ)
File "c:\users\user\appdata\local\programs\python\python38\lib\asyncio\events.py", line 501, in add_reader
raise NotImplementedError
NotImplementedError
Run Code Online (Sandbox Code Playgroud)
我知道ProactorEventLoop默认情况下Windows上的Python 3.8已切换为默认设置,因此我怀疑它与此相关。
Jupyter目前不支持Python 3.8?有没有解决的办法?
给定整数数组
[1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5]
Run Code Online (Sandbox Code Playgroud)
我需要掩盖重复N多次的元素。需要说明的是:主要目标是检索布尔掩码数组,以后再用于装箱计算。
我想出了一个相当复杂的解决方案
[1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5]
Run Code Online (Sandbox Code Playgroud)
给例如
import numpy as np
bins = np.array([1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5])
N = 3
splits = np.split(bins, np.where(np.diff(bins) != 0)[0]+1) …Run Code Online (Sandbox Code Playgroud) 检查 PyPI 包名称是否被占用的最佳方法是什么?
我已经多次在pypi中搜索软件包但没有收到任何结果。然后我花时间创建了一个包,其名称似乎没有被占用,但在部署时却收到错误。有没有更好的方法来检查包名称的可用性?
查看zoneinfoPython 3.9 中的模块,我想知道它是否还提供了一个方便的选项来检索 Windows 上的本地时区(操作系统设置)。
在 GNU/Linux 上,你可以这样做
\nfrom datetime import datetime\nfrom zoneinfo import ZoneInfo\n\nnaive = datetime(2020, 6, 11, 12)\naware = naive.replace(tzinfo=ZoneInfo(\'localtime\'))\nRun Code Online (Sandbox Code Playgroud)\n但在 Windows 上,这会抛出
\n\n\nZoneInfoNotFoundError: \'未找到包含关键本地时间的时区\'
\n
那么我还需要使用第三方库吗?例如
\nimport time\nimport dateutil\n\ntzloc = dateutil.tz.gettz(time.tzname[time.daylight])\naware = naive.replace(tzinfo=tzloc)\nRun Code Online (Sandbox Code Playgroud)\n由于time.tzname[time.daylight]返回本地化名称(在我的例子中为德语,例如 \'Mitteleurop\xc3\xa4ische Sommerzeit\'),因此这也不起作用:
aware = naive.replace(tzinfo=ZoneInfo(tzloc))\nRun Code Online (Sandbox Code Playgroud)\n有什么想法吗?
\nps 要在 Python < 3.9 上尝试此操作,请使用backports(另请参阅此答案):
pip install backports.zoneinfo\npip install tzdata # needed on Windows\nRun Code Online (Sandbox Code Playgroud)\n 我有两个想要比较的变量。打印出来后,它们是这样的:
2020-05-20 13:01:30
2020-05-20 14:49:03
Run Code Online (Sandbox Code Playgroud)
但是,一种是字符串类型,另一种是日期时间类型。如果我想将字符串一转换为日期类型以便我可以比较它们,这是唯一使用的方法吗strptime?因为这对我来说似乎有点多余,因为该字符串已经具有我想要的确切格式。基本上,是否有一个功能与 相同strptime,但不重新格式化它?正如您可以想象的那样,谷歌搜索这个问题是不可能的,因为我得到的只是人们试图将任何类型的字符串格式化为日期时间,所以所有答案都只是指向strptime.
我正在使用以下命令更新我的 git 存储库
python3 helper.py --更新 SomeName
但它显示错误,例如:
Traceback (most recent call last):
File "helper.py", line 13, in <module>
import git
ModuleNotFoundError: No module named 'git'
Run Code Online (Sandbox Code Playgroud)
我已经安装了 Python 3 (3.8.5) 和 Python 2 (2.7.18)。
有没有一种优雅的方法来重新编码极坐标数据框中的值。
例如
1->0,
2->0,
3->1...
Run Code Online (Sandbox Code Playgroud)
在 Pandas 中,它很简单:
df.replace([1,2,3,4,97,98,99],[0,0,1,1,2,2,2])
Run Code Online (Sandbox Code Playgroud) 我有一个数据框df,它的第一列是timedelta64
df.info():
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 686 entries, 0 to 685
Data columns (total 6 columns):
0 686 non-null timedelta64[ns]
1 686 non-null object
2 686 non-null object
3 686 non-null object
4 686 non-null object
5 686 non-null object
Run Code Online (Sandbox Code Playgroud)
print(df[0][2])例如,如果 I ,它会给我0 days 05:01:11。但是,我不想要0 days归档。我只想05:01:11被打印。有人可以教我如何做到这一点吗?非常感谢!
是否有任何内置函数polars或更好的方法通过定义时间分辨率(例如:天、小时、分钟)将持续时间转换为数字?
# Create a dataframe
df = pl.DataFrame(
{
"from": ["2023-01-01", "2023-01-02", "2023-01-03"],
"to": ["2023-01-04", "2023-01-05", "2023-01-06"],
}
)
# Convert to date and calculate the time difference
df = df.with_columns(
[
pl.col("from").str.strptime(pl.Date, "%Y-%m-%d").alias("from_date"),
pl.col("to").str.strptime(pl.Date, "%Y-%m-%d").alias("to_date"),
]
).with_columns((pl.col("to_date") - pl.col("from_date")).alias("time_diff"))
# Convert the time difference to int (in days)
df = df.with_columns(
((pl.col("time_diff") / (24 * 60 * 60 * 1000)).cast(pl.Int8)).alias("time_diff_int")
)
Run Code Online (Sandbox Code Playgroud)