Problem with numpy 2D Histogram

Peter Otten __peter__ at web.de
Fri Jun 20 08:30:32 EDT 2014


Jamie Mitchell wrote:

> On Friday, June 20, 2014 12:00:15 PM UTC+1, Peter Otten wrote:
>> Jamie Mitchell wrote:
>> 
>> 
>> 
>> > I have changed my x and y data to float64 types but I am still getting
>> > the
>> 
>> > same error message?
>> 
>> 
>> 
>> Please double-check by adding
>> 
>> 
>> 
>> assert x.dtype == np.float64
>> 
>> assert y.dtype == np.float64
>> 
>> 
>> 
>> If none of these assertions fail try to make a minimal script including
>> some
>> 
>> data that provokes the TypeError and post it here.
> 
> OK this is my code:
> 
> 
swh_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_annavg.nc','r')
> hs_Q0_con_sw=swh_Q0_con_sw.variables['hs'][:]
> x=hs_Q0_con_sw.astype(float64)
> # When I print the dtype of x here it says 'float64'
> 
mwp_Q0_con_sw=netCDF4.Dataset('/data/cr1/jmitchel/Q0/mean_wave_period/south_west/controlperiod/mwpcontrol_swest_annavg1D.nc','r')
> te_Q0_con_sw=mwp_Q0_con_sw.variables['te'][:]
> y=te_Q0_con_sw.astype(float64)
> If I try assert x.dtype == np.float64 I get:
> AssertionError

That means the dtype is not np.float64. As I have neither the data nor the 
netCDF4 library to replicate your problem I'm out.

What you might try is to construct a numpy array explicitly

x = np.array([float(v) for v in swh_Q0_con_sw.variables["hs"]])

but that's just a stab in the dark.

> hist,xedges,yedges=np.histogram2d(x,y,bins=10)
> TypeError: Cannot cast array data from dtype('O') to dtype('float64')
> according to the rule 'safe'
> 
> Thanks,
> 
> Jamie





More information about the Python-list mailing list