文件读写

In [1]:
import pandas as pd
In [2]:
data = pd.read_csv('data/sample.txt', sep='|')
In [3]:
data
Out[3]:
obsid designation obsdate lmjd planid spid fiberid ra dec snru ... mag5 mag6 mag7 tsource fibertype tfrom t_info rv z z_err
0 101001 J220848.54-020324.3 2011-10-24 55859 F5902 1 1 332.202274 -2.056767 2.23 ... 15.97 99 99 JF_LEGAS_S Obj SDSS_S NaN -23.069030 NaN 0.000003
1 101002 J220953.17-020506.0 2011-10-24 55859 F5902 1 2 332.471576 -2.085015 2.00 ... 15.67 99 99 JF_LEGAS_S Obj SDSS_S NaN 27.100000 NaN 0.000178
2 101008 J220928.49-015720.7 2011-10-24 55859 F5902 1 8 332.368745 -1.955771 1.84 ... 15.64 99 99 JF_LEGAS_S Obj SDSS_S NaN 25.038666 NaN 0.000003
3 101009 J220849.59-015207.1 2011-10-24 55859 F5902 1 9 332.206665 -1.868653 1.86 ... 16.25 99 99 JF_LEGAS_S Obj SDSS_S NaN -22.169652 NaN 0.000005
4 101016 J220923.69-020809.9 2011-10-24 55859 F5902 1 16 332.348725 -2.136096 2.17 ... 14.62 99 99 JF_LEGAS_S Obj SDSS_S NaN -6.631409 NaN 0.000001
5 101017 J220946.66-015526.5 2011-10-24 55859 F5902 1 17 332.444417 -1.924046 2.60 ... 15.65 99 99 JF_LEGAS_S Obj SDSS_S NaN -2.461296 NaN 0.000002
6 101020 J220853.37-015915.4 2011-10-24 55859 F5902 1 20 332.222379 -1.987626 2.65 ... 15.27 99 99 JF_LEGAS_S Obj SDSS_S NaN 10.849489 NaN 0.000008
7 101021 J220924.33-014833.5 2011-10-24 55859 F5902 1 21 332.351381 -1.809333 6.05 ... 14.92 99 99 JF_LEGAS_S Obj SDSS_S NaN -17.918595 NaN 0.000004
8 101023 J221001.52-020100.8 2011-10-24 55859 F5902 1 23 332.506374 -2.016900 2.35 ... 16.12 99 99 JF_LEGAS_S Obj SDSS_S NaN 52.658545 NaN 0.000002

9 rows × 32 columns

In [ ]:
data['obsid']
In [ ]:
data['designation']
In [4]:
data['obsdate']
Out[4]:
0    2011-10-24
1    2011-10-24
2    2011-10-24
3    2011-10-24
4    2011-10-24
5    2011-10-24
6    2011-10-24
7    2011-10-24
8    2011-10-24
Name: obsdate, dtype: object
In [5]:
import numpy as np
from astropy.table import Table


a = [1, 4, 5]
b = [2.0, 5.0, 8.2]
c = ['x', 'y', 'z']
t = Table([a, b, c], names=('a', 'b', 'c'), meta={'name': 'first table'})

t
Out[5]:
<Table length=3>
abc
int64float64str1
12.0x
45.0y
58.2z
In [2]:
data_rows = [(1, 2.0, 'x'),
    (4, 5.0, 'y'),
    (5, 8.2, 'z')]

t = Table(rows=data_rows, names=('a', 'b', 'c'), meta={'name': 'first table'},
    dtype=('i4', 'f8', 'S1'))

t
Out[2]:
<Table length=3>
abc
int32float64bytes1
12.0x
45.0y
58.2z
In [3]:
t['b'].unit = 's'

t
Out[3]:
<Table length=3>
abc
s
int32float64bytes1
12.0x
45.0y
58.2z
In [5]:
t['b'].format = '7.3f'
In [6]:
t
Out[6]:
<Table length=3>
abc
s
int32float64bytes1
12.000x
45.000y
58.200z
In [7]:
t.colnames
Out[7]:
['a', 'b', 'c']
In [7]:
# 行索引

t[0:2]['b']
Out[7]:
<Column name='b' dtype='float64' length=2>
2.0
5.0
In [10]:
from astropy.time import Time
from astropy.coordinates import SkyCoord

tm = Time(['2000:002', '2002:345'])
sc = SkyCoord([10, 20], [-45, +40], unit='deg')
t = Table([tm, sc], names=['time', 'skycoord'])
t
Out[10]:
<Table length=2>
timeskycoord
deg,deg
objectobject
2000:002:00:00:00.00010.0,-45.0
2002:345:00:00:00.00020.0,40.0
In [13]:
from astropy.table import Table, Column

t = Table()
t['a'] = [1, 4]
t['b'] = Column([2.0, 5.0], unit='cm', description='Velocity')
t['c'] = ['x', 'y']

t = Table(names=('a', 'b', 'c'), dtype=('f4', 'i4', 'S2'))
t.add_row((1, 2.0, 'x'))
t.add_row((4, 5.0, 'y'))

t
Out[13]:
<Table length=2>
abc
float32int32bytes2
1.02x
4.05y
In [17]:
# Column

col = Column([1, 2], name='a')  # shape=(2,)
col = Column([[1, 2], [3, 4]], name='a')  # shape=(2, 2)
col = Column([1, 2], name='a', dtype=float)
col = Column(np.array([1, 2]), name='a')
col = Column(['hello', 'world'], name='a')
In [19]:
t.columns   # Dict of table columns (access by column name, index, or slice)
t.colnames  # List of column names
t.meta      # Dict of meta-data
len(t)      # Number of table rows
Out[19]:
3
In [ ]:
# access

t['a']       # Column 'a'
t['a'][1]    # Row 1 of column 'a'
t[1]         # Row obj for with row 1 values
t[1]['a']    # Column 'a' of row 1
t[2:5]       # Table object with rows 2:5
t[[1, 3, 4]]  # Table object with rows 1, 3, 4 (copy)
t[np.array([1, 3, 4])]  # Table object with rows 1, 3, 4 (copy)
t[[]]        # Same table definition but with no rows of data
t['a', 'c']  # Table with cols 'a', 'c' (copy)
dat = np.array(t)  # Copy table data to numpy structured array object
t['a'].quantity  # an astropy.units.Quantity for Column 'a'
t['a'].to('km')  # an astropy.units.Quantity for Column 'a' in units of kilometers
t.columns[1]  # Column 1 (which is the 'b' column)
t.columns[0:2]  # New table with columns 0 and 1
In [ ]:
##

print(t)     # Print formatted version of table to the screen
t.pprint()   # Same as above
t.pprint(show_unit=True)  # Show column unit
t.pprint(show_name=False)  # Do not show column names
t.pprint(max_lines=-1, max_width=-1)  # Print full table no matter how long / wide it is

t.more()  # Interactively scroll through table like Unix "more"

print(t['a'])    # Formatted column values
t['a'].pprint()  # Same as above, with same options as Table.pprint()
t['a'].more()    # Interactively scroll through column

lines = t.pformat()  # Formatted table as a list of lines (same options as pprint)
lines = t['a'].pformat()  # Formatted column values as a list
In [23]:
t['b'].format = "%6.3f" 
t['b'].unit = 'm sec^-1'
t
Out[23]:
<Table length=3>
abc
m sec^-1
int64float64str1
12.000x
45.000y
58.200z
In [26]:
t.info('stats')
<Table length=3>
name      mean          std      min max
---- ------------- ------------- --- ---
   a 3.33333333333  1.6996731712   1   5
   b 5.06666666667 2.53157833947 2.0 8.2
   c            --            --  --  --
/Users/hebl/anaconda/lib/python3.5/site-packages/astropy/table/column.py:268: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  return self.data.__eq__(other)
In [27]:
t['b'].info
Out[27]:
name = b
dtype = float64
unit = m sec^-1
format = %6.3f
class = Column
n_bad = 0
length = 3
In [8]:
!head data/catalog
   1          BD+44 4550      3 36042          46           000001.1+444022000509.9+451345114.44-16.88 6.70  +0.07 +0.08         A1Vn               -0.012-0.018      -018      195  4.2  21.6AC   3
   2          BD-01 4525      6128569                       235956.2-010330000503.8-003011 98.33-61.14 6.29  +1.10 +1.02        gG9                 +0.045-0.060      +014V
   3 33    PscBD-06 6357     281285721002I         Var?     000013.0-061601000520.1-054227 93.75-65.93 4.61  +1.04 +0.89 +0.54   K0IIIbCN-0.5       -0.009+0.089 +.014-006SB1O < 17  2.5   0.0     3*
   4 86    PegBD+12 5063     87 917012004                   000033.8+125023000542.0+132346106.19-47.98 5.51  +0.90               G5III              +0.045-0.012      -002V?
   5          BD+57 2865    123 21085          61  V640 Cas 000101.8+575245000616.0+582612117.03-03.92 5.96  +0.67 +0.20         G5V                +0.263+0.030 +.047-012V          0.8   1.4      *
   6          CD-4914337    142214963      W                000108.4-493751000619.0-490430321.61-66.38 5.70  +0.52 +0.05         G1IV               +0.565-0.038 +.050+003SB         5.7   5.4      *
   7 10    CasBD+63 2107    144 109782005                   000114.4+633822000626.5+641146118.06  1.75 5.59  -0.03 -0.19         B9III             e+0.008 0.000      -000V     153                 *
   8          BD+28 4704    166 73743          69     33    000125.2+282811000636.8+290117111.26-32.83 6.13  +0.75 +0.33         K0V                +0.380-0.182 +.067-008V          2.6 158.6AB   4*
   9          CD-23    4    2031660531003                   000143.0-233947000650.1-230627 52.21-79.14 6.18  +0.38 +0.05         A7V                +0.100-0.045      +003V
  10          BD-18 6428    256147090                       000211.8-175639000718.2-172311 74.36-75.90 6.19  +0.14 +0.10         A6Vn               -0.018+0.036      -009V?    195                 *
In [31]:
##IO 

bsc = Table.read("data/catalog",
                readme="http://vizier.china-vo.org/ftp/cats/V/50/ReadMe",
               format="ascii.cds")
bsc
Downloading http://vizier.china-vo.org/ftp/cats/V/50/ReadMe [Done]
Out[31]:
<Table masked=True length=9110>
HRNameDMHDSAOFK5IRflagr_IRflagMultipleADSADScompVarIDRAh1900RAm1900RAs1900DE-1900DEd1900DEm1900DEs1900RAhRAmRAsDE-DEdDEmDEsGLONGLATVmagn_Vmagu_VmagB-Vu_B-VU-Bu_U-BR-In_R-ISpTypen_SpTypepmRApmDEn_ParallaxParallaxRadVeln_RadVell_RotVelRotVelu_RotVelDmagSepMultIDMultCntNoteFlag
hminsdegarcminarcsechminsdegarcminarcsecdegdegmagmagmagmagarcsec / yrarcsec / yrarcseckm / skm / smagarcsec
int64str10str11int64int64int64str1str1str1str5str2str9int64int64float64str1int64int64int64int64int64float64str1int64int64int64float64float64float64str1str1float64str1float64str1float64str1str18str1float64float64str1float64int64str4str2int64str1float64float64str4int64str1
1--BD+44 4550336042--------46----001.1+444022059.9+451345114.44-16.886.7----0.07--0.08------A1Vn---0.012-0.018-----18----195--4.221.6AC3--
2--BD-01 45256128569--------------235956.2-1330053.8-0301198.33-61.146.29----1.1--1.02------gG9--0.045-0.06----14V----------------
333 PscBD-06 6357281285721002I--------Var?0013.0-61610520.1-5422793.75-65.934.61----1.04--0.89--0.54--K0IIIbCN-0.5---0.0090.089--0.014-6SB1O<17--2.50.0--3*
486 PegBD+12 506387917012004------------0033.8+1250230542.0+132346106.19-47.985.51----0.9----------G5III--0.045-0.012-----2V?----------------
5--BD+57 286512321085--------61--V640 Cas011.8+5752450616.0+582612117.03-3.925.96----0.67--0.2------G5V--0.2630.03--0.047-12V------0.81.4----*
6--CD-4914337142214963------W------018.4-4937510619.0-49430321.61-66.385.7----0.52--0.05------G1IV--0.565-0.038--0.053SB------5.75.4----*
710 CasBD+63 2107144109782005------------0114.4+6338220626.5+641146118.061.755.59-----0.03---0.19------B9IIIe0.0080.0----0V--153----------*
8--BD+28 470416673743--------69--330125.2+2828110636.8+29117111.26-32.836.13----0.75--0.33------K0V--0.38-0.182--0.067-8V------2.6158.6AB4*
9--CD-23 42031660531003------------0143.0-2339470650.1-2362752.21-79.146.18----0.38--0.05------A7V--0.1-0.045----3V----------------
...............................................................................................................................................................
9101--BD-17 6868225197147064--------------235912.4-17530419.7-16314474.7-74.795.78----1.1--1.05------K2III--0.039-0.059-----27------------------
9102--CD-2918950225200166031--------------235913.2-2949330420.3-2916718.53-79.416.4----0.0---0.05------A0V--0.030.014----28------------------
91033 CetBD-11 61942252121470662001I--------13235923.0-113580430.1-10303487.07-70.044.94----1.63--1.92------K3Ibv-0.005-0.011--0.016-42V<17------------
9104--BD+66 167922521610956--------------235930.1+6636320442.0+67100118.414.715.67----1.07--0.93------K1III--0.0920.03--0.013-27------------------
9105--BD+41 493322521836037--I----30----235928.2+4132100436.7+42532113.72-19.946.01R--------------B9III--0.005-0.022-----8SB------3.75.4----*
9106--CP-73 2346225233255629--------------235926.8-7327120430.7-725352307.68-43.87.31----0.44--0.01------F2V--0.01-0.054----8------------------
9107--BD+33 4828225239536222002------------235939.2+34620453.8+343935112.17-27.246.12----0.62--0.09------G2V--0.7720.089--0.0354V----------------
9108--CP-72 28002252532556311001------------235937.0-7159360441.3-712613308.18-45.215.59-----0.12---0.42------B8IV-V--0.028-0.014-----3V----------------
9109--BD+25 506822527673731--I----42----235947.1+265330456.0+263856110.22-35.076.25----1.4--1.59------K4IIIb--0.11-0.012-----5--------4.517.5------
9110--BD+60 266722528910962------------V567 Cas235955.9+604526056.2+611851117.4-1.065.8-----0.09---0.32------B8IVpHgMn--0.0150.005----14V--50----------*
Format Read Write Auto-identify Deprecated
aastex Yes Yes No Yes
ascii Yes Yes No  
ascii.aastex Yes Yes No  
ascii.basic Yes Yes No  
ascii.cds Yes No No  
ascii.commented_header Yes Yes No  
ascii.daophot Yes No No  
ascii.ecsv Yes Yes No  
ascii.fixed_width Yes Yes No  
ascii.fixed_width_no_header Yes Yes No  
ascii.fixed_width_two_line Yes Yes No  
ascii.html Yes Yes Yes  
ascii.ipac Yes Yes No  
ascii.latex Yes Yes Yes  
ascii.no_header Yes Yes No  
ascii.rdb Yes Yes Yes  
ascii.sextractor Yes No No  
ascii.tab Yes Yes No  
ascii.csv Yes Yes Yes  
cds Yes No No Yes
daophot Yes No No Yes
fits Yes Yes Yes  
hdf5 Yes Yes Yes  
html Yes Yes No Yes
ipac Yes Yes No Yes
latex Yes Yes No Yes
rdb Yes Yes No Yes
votable Yes Yes Yes  
In [ ]:
t = Table.read('aj285677t3_votable.xml')
In [ ]: