为什么列类型不能像转换器设置中那样读取?
Why the column type can't read as in converters's setting?
我想读取指定列的字符串类型的csv文件,数据文件位于:
请下载并另存为$HOME\cbond.csv
(由于GFW无法上传到dropbox和其他网盘,建国云提供英文gui,创建你自己的免费帐户并下载我的示例数据文件)。
import pandas as df
df = pd.read_csv('cbond.csv',sep=',',header=0, converters={'正股代码':str})
我将 csv 文件中的列 正股代码
设置为带有转换器的字符串类型,使用 df.info()
检查所有列的数据类型。
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 239 entries, 0 to 238
Data columns (total 17 columns):
代码 239 non-null int64
转债名称 239 non-null object
现价 239 non-null float64
涨跌幅 239 non-null float64
正股名称 239 non-null object
正股价 239 non-null float64
正股涨跌 239 non-null float64
转股价 239 non-null float64
回售触发价 239 non-null float64
强赎触发价 239 non-null float64
到期时间 239 non-null object
剩余年限 239 non-null float64
正股代码 239 non-null object
转股起始日 239 non-null object
发行规模 239 non-null float64
剩余规模 239 non-null object
转股溢价率 239 non-null float64
dtypes: float64(10), int64(1), object(6)
为什么 正股代码
列显示为
正股代码 239 non-null object
而不是
正股代码 239 non-null string
?
升级pandas:
sudo apt-get install --upgrade python3-pandas
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pandas is already the newest version (0.19.2-5.1).
尝试不同的语句:
>>> import pandas as pd
>>> pd.__version__
'0.24.2'
>>> test_1 = pd.read_csv('cbond.csv',dtype={'正股代码':'string'})
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/pandas/core/dtypes/common.py", line 2011, in pandas_dtype
npdtype = np.dtype(dtype)
TypeError: data type "string" not understood
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 702, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 429, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 1122, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 1853, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 490, in pandas._libs.parsers.TextReader.__cinit__
File "/usr/local/lib/python3.5/dist-packages/pandas/core/dtypes/common.py", line 2017, in pandas_dtype
dtype))
TypeError: data type 'string' not understood
>>> test_2 = pd.read_csv('cbond.csv',dtype={'正股代码':'str'})
>>> test_2.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 239 entries, 0 to 238
Data columns (total 17 columns):
代码 239 non-null int64
转债名称 239 non-null object
现价 239 non-null float64
涨跌幅 239 non-null float64
正股代码 239 non-null object
正股名称 239 non-null object
正股价 239 non-null float64
正股涨跌 239 non-null float64
转股价 239 non-null float64
回售触发价 239 non-null float64
强赎触发价 239 non-null float64
到期时间 239 non-null object
剩余年限 239 non-null float64
转股起始日 239 non-null object
发行规模 239 non-null float64
剩余规模 239 non-null object
转股溢价率 239 non-null float64
dtypes: float64(10), int64(1), object(6)
memory usage: 31.8+ KB
读取 csv 文件后分配列的 dtype 是否有帮助?
df['正股代码'] = df['正股代码'].astype('string')
对于新的 pandas 1.0,String dtype 正在试验中。
在这里阅读更多:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.StringDtype.html#pandas.StringDtype
这对我有用:
test_df = pd.DataFrame(data={'numbers_column':np.nan,
'strings_column':['3_re', '4_re', '5_re','random_str']},
index=[1,2,3, 4])
## until here the dtype of strings_column is still object
test_df['strings_column'] = test_df['strings_column'].astype('string')
或者在打开文件时立即将其作为字符串读取,这对我有用:
test_2 = pd.read_csv(.....,
dtype={'正股代码':'string'})
在 pandas 1.0.0
之前,即您的版本 0.19
,
pandas 中没有 dtype
string
,可能是来自 numpy
的内部 np.str
或 StringArray
。
df.info()
将其视为对象 dtype
https://pandas.pydata.org/pandas-docs/stable/user_guide/text.html#text-data-types
使用 convert_dtypes
,需要 Pandas>=1.0.2,它支持使用支持 pd.NA
.
的数据类型将列转换为最佳可能的数据类型
文档:pandas.DataFrame.convert_dtypes
试试这个:
import pandas as pd
df = pd.read_csv('cbond.csv')
dfn = df.convert_dtypes()
print(dfn)
"""
代码 Int64
转债名称 string
现价 float64
涨跌幅 float64
正股名称 string
正股价 float64
正股涨跌 float64
转股价 float64
回售触发价 float64
强赎触发价 float64
到期时间 string
剩余年限 float64
正股代码 Int64
转股起始日 string
发行规模 float64
剩余规模 string
转股溢价率 float64
dtype: object
"""
此外,为什么 df = pd.read_csv('cbond.csv',sep=',',header=0, converters={'正股代码':str})
或 df['正股代码'] = df['正股代码'].astype('string')
不能如我们所愿地工作?
这似乎是 me/us 的错误,但 pandas 的功能。
无论如何,convert_dtypes
已经为我解决了这个问题。
我想读取指定列的字符串类型的csv文件,数据文件位于:
请下载并另存为$HOME\cbond.csv
(由于GFW无法上传到dropbox和其他网盘,建国云提供英文gui,创建你自己的免费帐户并下载我的示例数据文件)。
import pandas as df
df = pd.read_csv('cbond.csv',sep=',',header=0, converters={'正股代码':str})
我将 csv 文件中的列 正股代码
设置为带有转换器的字符串类型,使用 df.info()
检查所有列的数据类型。
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 239 entries, 0 to 238
Data columns (total 17 columns):
代码 239 non-null int64
转债名称 239 non-null object
现价 239 non-null float64
涨跌幅 239 non-null float64
正股名称 239 non-null object
正股价 239 non-null float64
正股涨跌 239 non-null float64
转股价 239 non-null float64
回售触发价 239 non-null float64
强赎触发价 239 non-null float64
到期时间 239 non-null object
剩余年限 239 non-null float64
正股代码 239 non-null object
转股起始日 239 non-null object
发行规模 239 non-null float64
剩余规模 239 non-null object
转股溢价率 239 non-null float64
dtypes: float64(10), int64(1), object(6)
为什么 正股代码
列显示为
正股代码 239 non-null object
而不是
正股代码 239 non-null string
?
升级pandas:
sudo apt-get install --upgrade python3-pandas
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pandas is already the newest version (0.19.2-5.1).
尝试不同的语句:
>>> import pandas as pd
>>> pd.__version__
'0.24.2'
>>> test_1 = pd.read_csv('cbond.csv',dtype={'正股代码':'string'})
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/pandas/core/dtypes/common.py", line 2011, in pandas_dtype
npdtype = np.dtype(dtype)
TypeError: data type "string" not understood
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 702, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 429, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 1122, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/usr/local/lib/python3.5/dist-packages/pandas/io/parsers.py", line 1853, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 490, in pandas._libs.parsers.TextReader.__cinit__
File "/usr/local/lib/python3.5/dist-packages/pandas/core/dtypes/common.py", line 2017, in pandas_dtype
dtype))
TypeError: data type 'string' not understood
>>> test_2 = pd.read_csv('cbond.csv',dtype={'正股代码':'str'})
>>> test_2.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 239 entries, 0 to 238
Data columns (total 17 columns):
代码 239 non-null int64
转债名称 239 non-null object
现价 239 non-null float64
涨跌幅 239 non-null float64
正股代码 239 non-null object
正股名称 239 non-null object
正股价 239 non-null float64
正股涨跌 239 non-null float64
转股价 239 non-null float64
回售触发价 239 non-null float64
强赎触发价 239 non-null float64
到期时间 239 non-null object
剩余年限 239 non-null float64
转股起始日 239 non-null object
发行规模 239 non-null float64
剩余规模 239 non-null object
转股溢价率 239 non-null float64
dtypes: float64(10), int64(1), object(6)
memory usage: 31.8+ KB
读取 csv 文件后分配列的 dtype 是否有帮助?
df['正股代码'] = df['正股代码'].astype('string')
对于新的 pandas 1.0,String dtype 正在试验中。 在这里阅读更多:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.StringDtype.html#pandas.StringDtype
这对我有用:
test_df = pd.DataFrame(data={'numbers_column':np.nan,
'strings_column':['3_re', '4_re', '5_re','random_str']},
index=[1,2,3, 4])
## until here the dtype of strings_column is still object
test_df['strings_column'] = test_df['strings_column'].astype('string')
或者在打开文件时立即将其作为字符串读取,这对我有用:
test_2 = pd.read_csv(.....,
dtype={'正股代码':'string'})
在 pandas 1.0.0
之前,即您的版本 0.19
,
pandas 中没有 dtype
string
,可能是来自 numpy
的内部 np.str
或 StringArray
。
df.info()
将其视为对象 dtype
https://pandas.pydata.org/pandas-docs/stable/user_guide/text.html#text-data-types
使用 convert_dtypes
,需要 Pandas>=1.0.2,它支持使用支持 pd.NA
.
文档:pandas.DataFrame.convert_dtypes
试试这个:
import pandas as pd
df = pd.read_csv('cbond.csv')
dfn = df.convert_dtypes()
print(dfn)
"""
代码 Int64
转债名称 string
现价 float64
涨跌幅 float64
正股名称 string
正股价 float64
正股涨跌 float64
转股价 float64
回售触发价 float64
强赎触发价 float64
到期时间 string
剩余年限 float64
正股代码 Int64
转股起始日 string
发行规模 float64
剩余规模 string
转股溢价率 float64
dtype: object
"""
此外,为什么 df = pd.read_csv('cbond.csv',sep=',',header=0, converters={'正股代码':str})
或 df['正股代码'] = df['正股代码'].astype('string')
不能如我们所愿地工作?
这似乎是 me/us 的错误,但 pandas 的功能。
无论如何,convert_dtypes
已经为我解决了这个问题。