相关疑难解决方法(0)

具有混合数据类型的 pandas DataFrame 的类型提示

我一直在寻找 pandas DataFrame 的健壮类型提示,但似乎找不到任何有用的东西。这个问题仅仅触及了表面Pythonic 类型提示与 pandas?

通常,如果我想暗示以 DataFrame 作为输入参数的函数的类型,我会这样做:

import pandas as pd 
def func(arg: pd.DataFrame) -> int: 
     return 1
Run Code Online (Sandbox Code Playgroud)

我似乎找不到的是如何输入具有混合 dtypes 的DataFrame 提示。DataFrame 构造函数仅支持完整 DataFrame 的类型定义。因此,据我所知,数据类型的更改只能在该pd.DataFrame().astype(dtypes={})函数之后发生。

这在这里有效,但对我来说似乎不太Pythonic

import datetime
def func(arg: pd.DataFrame(columns=['integer', 'date']).astype(dtype={'integer': int, 'date': datetime.date})) -> int:
    return 1
Run Code Online (Sandbox Code Playgroud)

我遇到了这个包: https: //pypi.org/project/dataenforce/,其中包含如下示例:

def process_data(data: Dataset["id": int, "name": object, "latitude": float, "longitude": float])
  pass
Run Code Online (Sandbox Code Playgroud)

这看起来很有希望,但遗憾的是该项目已经过时且存在缺陷。

作为一名数据科学家,在构建具有长 ETL 流程的机器学习应用程序时,我认为类型提示非常重要。

你用什么,有人在 pandas 中输入暗示他们的数据框吗?

python type-hinting pandas

10
推荐指数
1
解决办法
9600
查看次数

NumPy ndarray dtype的类型提示?

我想要一个函数在NumPy ndarray的旁边加上类型提示dtype

例如,使用列表,可以执行以下操作...

def foo(bar: List[int]):
   ...
Run Code Online (Sandbox Code Playgroud)

...以给出bar必须list由组成的类型提示int

不幸的是,此语法抛出NumPy异常ndarray

def foo(bar: np.ndarray[np.bool]):
   ...

> np.ndarray[np.bool]) (...) TypeError: 'type' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)

是否可以提供dtype特定于类型的提示np.ndarray

python numpy type-hinting python-3.6

7
推荐指数
4
解决办法
1459
查看次数

如何将 PySpark 函数的返回类型指定为数据帧?

我最近正在解决一些编码挑战,涉及将 Spark 数据帧传递到 Python 函数并返回一个新的数据帧。我记得的语法是这样的:

def sampleFunction(df: Dataframe) -> Dataframe:
    * do stuff *
    return newDF
Run Code Online (Sandbox Code Playgroud)

我现在正在尝试创建自己的示例,但无法将数据帧指定为输入/输出类型。我假设我需要导入一些东西来使 dataframe 成为可接受的类型,但在过去的一个小时里我一直在 Google 上搜索这个内容,但我找不到一个关于如何在 PySpark 中实现此功能的示例。

python function dataframe pyspark

4
推荐指数
1
解决办法
1万
查看次数