Differences between revisions 3 and 6 (spanning 3 versions)
Revision 3 as of 2012-10-24 15:15:20
Size: 507
Editor: MikhailItkin
Comment:
Revision 6 as of 2012-10-24 16:29:19
Size: 1022
Editor: MikhailItkin
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
 * Series
Line 7: Line 6:
 * Apply common numpy statistics
Line 12: Line 12:
import pandas as P import pandas as p
Line 15: Line 15:
hamNc = Nio.open_file('10147-precip.nc')
helNc = Nio.open_file('10015-precip.nc')
Line 18: Line 16:
hamTime = hamNc.variables['time'][:]
helTime = helNc.variables['time'][:]
nc1 = Nio.open_file('10147-precip.nc') # hamburg
nc2 = Nio.open_file('10015-precip.nc') # helgoland
Line 21: Line 19:
hamRain = hamNc.variables['rainfall_rain_rate'][:]
helRain = helNc.variables['rainfall_rain_rate'][:]
ham = hamNc.variables['rainfall_rain_rate'][:]
time1 = nc1.variables['time'][:]
time2 = nc2.variables['time'][:]

rain1 = nc1.variables['rainfall_rate_hour'][:]
rain2 = nc2.variables['rainfall_rate_hour'][:]


# plot data
plot(rain1, 'g', rain2, 'b')

# Timestamps shall be python dates
dates1 = num2date(epoch2num(time1))
dates2 = num2date(epoch2num(time2))

# Indexed arrays - p.Series
ds1 = p.Series(rain1, index = dates1)
ds2 = p.Series(rain2, index = dates2)

# Pandas is using numpy.na representation of not-a-number, while Nio returns masked arrays
# Many basic array operations are valid for pandas Series
ds1 = np.where(ds1<0, nan, ds1)
ds2 = np.where(ds2<0, nan, ds2)

# plotting functions
ds1.plot()
ds2.plot()

Pandas

  • Indexed arrays
  • DateFrame

  • DateRange

  • Indexing, slicing
  • Apply common numpy statistics
  • Data alignment

   1 import numpy as np
   2 import pandas as p
   3 import Nio
   4 
   5 
   6 nc1 = Nio.open_file('10147-precip.nc') # hamburg
   7 nc2 = Nio.open_file('10015-precip.nc') # helgoland
   8 
   9 time1 = nc1.variables['time'][:]
  10 time2 = nc2.variables['time'][:]
  11 
  12 rain1 = nc1.variables['rainfall_rate_hour'][:]
  13 rain2 = nc2.variables['rainfall_rate_hour'][:]
  14 
  15 
  16 # plot data 
  17 plot(rain1, 'g', rain2, 'b')
  18 
  19 # Timestamps shall be python dates
  20 dates1 = num2date(epoch2num(time1))
  21 dates2 = num2date(epoch2num(time2))
  22 
  23 # Indexed arrays - p.Series
  24 ds1 = p.Series(rain1, index = dates1)
  25 ds2 = p.Series(rain2, index = dates2)
  26 
  27 # Pandas is using numpy.na representation of not-a-number, while Nio returns masked arrays
  28 # Many basic array operations are valid for pandas Series
  29 ds1 = np.where(ds1<0, nan, ds1)
  30 ds2 = np.where(ds2<0, nan, ds2)
  31 
  32 # plotting functions
  33 ds1.plot()
  34 ds2.plot()

LehreWiki: PythonCourse/PythonLES/Pandas (last edited 2012-11-05 10:53:39 by anonymous)