pandas 'NoneType' 对象没有属性 'fileno'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45126368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:01:13  来源:igfitidea点击:

'NoneType' object has no attribute 'fileno'

pythonpandasdataframe

提问by frank

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime

plt.style.use('ggplot')
columns = ['user_id','order_dt','order_products','order_amount']
df = pd.read_csv('CDNOW_master.txt',names = columns,sep = '\s+')
df['order_date'] = pd.to_datetime(df.order_dt,format='%Y%m%d')
df['month'] = df.order_date.values.astype('datetime64[M]')
f = df.groupby('user_id')['month'].min().value_counts()
print(f)

Above is my code,my purpose is to get the value_counts of the users purchased in their first month, but only got the result of 'NoneType' object has no attribute 'fileno'.

以上是我的代码,我的目的是获取第一个月购买的用户的value_counts,但只得到'NoneType'对象没有属性'fileno'的结果。

any ideas? much appreciate

有任何想法吗?非常感谢

here are the traceback

这是回溯

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\practice\CDNOW.py", line 19, in <module>
    print(f)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pandas\core\base.py", line 51, in __str__
    return self.__unicode__()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pandas\core\series.py", line 982, in __unicode__
    width, height = get_terminal_size()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pandas\io\formats\terminal.py", line 33, in get_terminal_size
    return shutil.get_terminal_size()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 1071, in get_terminal_size
    size = os.get_terminal_size(sys.__stdout__.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'

回答by chris dorn

I am seeing this as well.

我也看到了这个。

>>> type(sys.__stdout__)
<class 'NoneType'>

I get NoneType when calling dunder stdout while I am using idle. I assume that pandas wants to determine how much to display in the results and is looking for the sys output information. In the documentation, it mentions what this is, but not how to reset it.

我在使用空闲时调用 dunder stdout 时得到 NoneType。我假设pandas 想要确定在结果中显示多少并且正在寻找sys 输出信息。在文档中,它提到了这是什么,但没有提到如何重置它。

I did this:

我这样做了:

sys.__stdout__ = sys.stdout

and it fixed the problem, but I have not idea if I caused problems down the line.

它解决了问题,但我不知道我是否导致了问题。

回答by Uddipan Paul

You may wish to try out the following.

您可能希望尝试以下方法。

df = pd.read_csv('CDNOW_master.txt',usecols = columns,sep = '\s+')

instead of

代替

df = pd.read_csv('CDNOW_master.txt',names = columns,sep = '\s+')

This solved my problem. Hope it solves yours too.

这解决了我的问题。希望它也能解决你的问题。