Thinking in Pandas by Hannah Stepanek

Thinking in Pandas by Hannah Stepanek

Author:Hannah Stepanek
Language: eng
Format: epub
ISBN: 9781484258392
Publisher: Apress


Listing 4-17Explicitly converting certain columns to datetime objects

Note Listing 4-17 assumes that there are no NaNs or placeholder values in the column. If there are, like in Listing 4-18, na_values must be specified to convert all the placeholder values to NaNs; otherwise, the column will be an object rather than a datetime because the placeholder values will be left as strings.>> data = io.StringIO(

"""

id,birth,height,weight

129237,04/10/1999,5.4,126

123083,unknown,6.1,150

123087,11/23/1989,4.5,111

"""

)

>> df = pd.read_csv(

data,

dtype={

'id': np.int32,

'height': np.float16,

'weight': np.int16},

parse_dates=["birth"],

na_values=["unknown"],

index_col=[0],

)

birth height weight

id

129237 1999-04-10 5.398438 26

123083 NaT 6.101562 150

123083 1989-11-23 6.101562 111

>> df.memory_usage(deep=True)

Index 24

birth 24

height 6

weight 6

>> df.dtypes

age int8

height float16

weight datetime64[ns]

>> df.index.dtype

dtype('int64')



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.