Changing data type with pandas on read_excel
Question: I'm looking for some help as I'm actually quite new to pandas (and python).
I'm facing a data type conversion problem with some datas.

As you can see (and try), I'm trying to tell pandas that I want it to read the
"DEP" data column as a string (because I want to keep the data unchanged)

    
    
    >>> df = pd.read_excel("http://www2.impots.gouv.fr/documentation/statistiques/ircom2003/dep/060.xls", 0, skiprows=23, na_values="n.d.")
    >>> df.dtypes
    Unnamed: 0                                                  float64
    DEP                                                         float64
    Commune                                                     float64
    ...
    
    >>> df["DEP"] = df["DEP"].astype(str)
    
    >>> df.dtypes
    Unnamed: 0                                                  float64
    DEP                                                          object
    Commune                                                     float64
    .....
    
    >>> df["DEP"][5]
    '60.0'
    

You can download the excel file if you want, but the input data look like this
: (I've added the slash between column names)

    
    
    DEP / Commune   / Libellé de la commune
    060 001 AIGLUN
    060 002 AMIRAT
    

In this case, I would like to simply keep the data "060" and "001" as strings.
I'm using python 3.4 and pandas 0.16

Thanks a lot for your help.


Answer: There is another possibility. I must admit it's a little bit scrapy, but I
tested successfully.

You need to create a new class:

    
    
    class NewType():
       def __init__(self, sValue=""):
            self.strValue = sValue
    
       def __str__(self):
            return self.strValue
    

then define a convert function:

    
    
    def convert(value):
        return NewType(value)
    

in your function (where you want to read_excel), do the following, assuming
you have 28 columns to read:

    
    
    import pandas as pd
    converters = dict()
    for i  in range(0,28,1):
        converters[i] = convert
    dataframe = pd.read_excel(path_to_file, sheet_name, 0, None, 0, None, 27, False, None, "", None, False, None, converters)
    dataframe = dataframe.transpose()
    dataDict = dataframe.to_dict() 
    newDict= OrderedDict()
    for dataLine in dataDict.values():
        for field in dataLine.keys():
            dataLine[field] = str(dataLine[field])
        #do something with dataLine
    

At the end you have something identical to CSV.DictReader if you had the data
formatted in CSV

