检查系列中每一行dtype的有效方法

时间:2019-03-03 09:36:06

标签: python pandas series

说我混合了ts /其他数据:

<div id="scenes">
	<h2>Szenen</h2>

	<mat-expansion-panel>
	  <mat-expansion-panel-header [expandedHeight]="'50px'"  [collapsedHeight]="'50px'" class="komfortScene" >

	    <mat-panel-title>
	    	<div class="matPanelSceneSymbol"><i class="material-icons md-36 md-light active">weekend</i></div>
	    	
	     	<div class="matPanelSceneText"> Testszene</div>
	    </mat-panel-title>
	    <mat-panel-description>
	      <div class="matPanelSceneText">Szenenbeschreibung</div>
	    </mat-panel-description>

	  </mat-expansion-panel-header>
	 <!--  <div class="szenenContainer"> -->
	  	<!-- <button mat-raised-button (click)="isLinear = !isLinear" id="toggle-linear">
  {{!isLinear ? 'Enable linear mode' : 'Disable linear mode'}}
</button> -->
<mat-vertical-stepper [linear]="isLinear" #stepper>
	<mat-step>
    <form>
      <ng-template matStepLabel>Funktion wählen</ng-template>
      <mat-button-toggle-group #group="matButtonToggleGroup" class="toggleTabGroup">
		  <mat-button-toggle value="left" class="toggleTab">
		   <i class="material-icons md-36 md-light active">weekend</i>
		   <span>Komfort</span>
		  </mat-button-toggle>
		  <mat-button-toggle value="center" class="toggleTab">
		   <i class="material-icons md-36 md-red">verified_user</i>
		   <span>Alarm</span>
		  </mat-button-toggle>
		  <mat-button-toggle value="right" class="toggleTab">
		   <i class="material-icons md-36 md-violet">access_time</i>
		   <span>Timer</span>
		  </mat-button-toggle>
		</mat-button-toggle-group>
    </form>
  </mat-step>
  <mat-step>
    <form>
      <ng-template matStepLabel>Szene benennen & beschreiben</ng-template>
      <mat-form-field>
        <input matInput placeholder="Name" required>
      </mat-form-field>
      <mat-form-field>
        <input matInput placeholder="Beschreibung" required>
      </mat-form-field>
  <!--     <div>
        <button mat-button matStepperNext></button>
      </div> -->
    </form>
  </mat-step>
  
  <mat-step>
    <form>
      <ng-template matStepLabel>Auslöser</ng-template>
      <mat-form-field>
			  <mat-select class="selectPanel" placeholder="Auslöser">
			  <!--   <mat-option>-- None --</mat-option> -->
			   <!--  <mat-optgroup *ngFor="let modules of ApiService.modulesForDevices" [label]="modules.modulename" -->
			   	<mat-optgroup label="Vorkonfigurierte Auslöser" >
			   		<mat-option class="preconfigOption1" value="test">
				        Alle Fensterkontakte
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Bewegungsmelder
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Rauchmelder
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Wassermelder
				      </mat-option>
			   	</mat-optgroup>

			      <div *ngFor="let modules of ApiService.modulesForDevices" >
				      <mat-option  class="matOption" *ngIf="modules.type === 2" [value]="modules.id">
				        {{modules.location}} - {{modules.description}}
				      </mat-option>
			      </div>

			<!--     </mat-optgroup> -->
			 </mat-select>

			</mat-form-field>
    </form>
  </mat-step>
  <mat-step>
    <form>
      <ng-template matStepLabel>Aktor</ng-template>
      <mat-form-field>
			  <mat-select  class="selectPanel" placeholder="Aktor">
			  <!--   <mat-option>-- None --</mat-option> -->
			   <!--  <mat-optgroup *ngFor="let modules of ApiService.modulesForDevices" [label]="modules.modulename" -->
			                 
			      <mat-option *ngFor="let modules of ApiService.modulesForDevices" [value]="modules.id">
			        {{modules.location}} {{modules.description}}
			      </mat-option>
			<!--     </mat-optgroup> -->
			 </mat-select>
			</mat-form-field>
    </form>
  </mat-step>
  <mat-step>
    <ng-template matStepLabel>Fertig</ng-template>
    Fertig?
    <div>
      <button mat-button matStepperPrevious>Zurück</button>
      <button mat-button (click)="stepper.reset()">Speichern</button>
    </div>
  </mat-step>
</mat-vertical-stepper>
	</mat-expansion-panel>


	<mat-expansion-panel>
	  <mat-expansion-panel-header [expandedHeight]="'50px'"  [collapsedHeight]="'50px'" class="timerScene">

	    <mat-panel-title>
	    	<div class="matPanelSceneSymbol"><i class="material-icons md-36 md-violet">access_time</i></div>
	    	
	     	<div class="matPanelSceneText"> Testszene</div>
	    </mat-panel-title>
	    <mat-panel-description>
	      <div class="matPanelSceneText">Szenenbeschreibung</div>
	    </mat-panel-description>

	  </mat-expansion-panel-header>
	 <!--  <div class="szenenContainer"> -->
	  	<!-- <button mat-raised-button (click)="isLinear = !isLinear" id="toggle-linear">
  {{!isLinear ? 'Enable linear mode' : 'Disable linear mode'}}
</button> -->
<mat-vertical-stepper [linear]="isLinear" #stepper>
	<mat-step>
    <form>
      <ng-template matStepLabel>Funktion wählen</ng-template>
      <mat-button-toggle-group #group="matButtonToggleGroup" class="toggleTabGroup">
		  <mat-button-toggle value="left" class="toggleTab">
		   <i class="material-icons md-36 md-light active">weekend</i>
		   <span>Komfort</span>
		  </mat-button-toggle>
		  <mat-button-toggle value="center" class="toggleTab">
		   <i class="material-icons md-36 md-red">verified_user</i>
		   <span>Alarm</span>
		  </mat-button-toggle>
		  <mat-button-toggle value="right" class="toggleTab">
		   <i class="material-icons md-36 md-violet">access_time</i>
		   <span>Timer</span>
		  </mat-button-toggle>
		</mat-button-toggle-group>
    </form>
  </mat-step>
  <mat-step>
    <form>
      <ng-template matStepLabel>Szene benennen & beschreiben</ng-template>
      <mat-form-field>
        <input matInput placeholder="Name" required>
      </mat-form-field>
      <mat-form-field>
        <input matInput placeholder="Beschreibung" required>
      </mat-form-field>
  <!--     <div>
        <button mat-button matStepperNext></button>
      </div> -->
    </form>
  </mat-step>
  
  <mat-step>
    <form>
      <ng-template matStepLabel>Auslöser</ng-template>
      <mat-form-field>
			  <mat-select class="selectPanel" placeholder="Auslöser">
			  <!--   <mat-option>-- None --</mat-option> -->
			   <!--  <mat-optgroup *ngFor="let modules of ApiService.modulesForDevices" [label]="modules.modulename" -->
			   	<mat-optgroup label="Vorkonfigurierte Auslöser" >
			   		<mat-option class="preconfigOption1" value="test">
				        Alle Fensterkontakte
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Bewegungsmelder
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Rauchmelder
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Wassermelder
				      </mat-option>
			   	</mat-optgroup>

			      <div *ngFor="let modules of ApiService.modulesForDevices" >
				      <mat-option  class="matOption" *ngIf="modules.type === 2" [value]="modules.id">
				        {{modules.location}} - {{modules.description}}
				      </mat-option>
			      </div>

			<!--     </mat-optgroup> -->
			 </mat-select>

			</mat-form-field>
    </form>
  </mat-step>
  <mat-step>
    <form>
      <ng-template matStepLabel>Aktor</ng-template>
      <mat-form-field>
			  <mat-select  class="selectPanel" placeholder="Aktor">
			  <!--   <mat-option>-- None --</mat-option> -->
			   <!--  <mat-optgroup *ngFor="let modules of ApiService.modulesForDevices" [label]="modules.modulename" -->
			                 
			      <mat-option *ngFor="let modules of ApiService.modulesForDevices" [value]="modules.id">
			        {{modules.location}} {{modules.description}}
			      </mat-option>
			<!--     </mat-optgroup> -->
			 </mat-select>
			</mat-form-field>
    </form>
  </mat-step>
  <mat-step>
    <!-- <ng-template matStepLabel>Fertig</ng-template>
    Fertig?
    <div>
      <button mat-button matStepperPrevious>Zurück</button>
      <button mat-button (click)="stepper.reset()">Speichern</button>
    </div> -->
  </mat-step>
</mat-vertical-stepper>
	</mat-expansion-panel>





	<mat-expansion-panel>
	  <mat-expansion-panel-header [expandedHeight]="'50px'"  [collapsedHeight]="'50px'" class="alarmScene">

	    <mat-panel-title>
	    	<div class="matPanelSceneSymbol"><i class="material-icons md-36 md-red">verified_user</i></div>
	    	
	     	<div class="matPanelSceneText"> Testszene</div>
	    </mat-panel-title>
	    <mat-panel-description>
	      <div class="matPanelSceneText">Szenenbeschreibung</div>
	    </mat-panel-description>

	  </mat-expansion-panel-header>
	 <!--  <div class="szenenContainer"> -->
	  	<!-- <button mat-raised-button (click)="isLinear = !isLinear" id="toggle-linear">
  {{!isLinear ? 'Enable linear mode' : 'Disable linear mode'}}
</button> -->
<mat-vertical-stepper [linear]="isLinear" #stepper>
	<mat-step>
    <form>
      <ng-template matStepLabel>Funktion wählen</ng-template>
      <mat-button-toggle-group #group="matButtonToggleGroup" class="toggleTabGroup">
		  <mat-button-toggle value="left" class="toggleTab">
		   <i class="material-icons md-36 md-light active">weekend</i>
		   <span>Komfort</span>
		  </mat-button-toggle>
		  <mat-button-toggle value="center" class="toggleTab">
		   <i class="material-icons md-36 md-red">verified_user</i>
		   <span>Alarm</span>
		  </mat-button-toggle>
		  <mat-button-toggle value="right" class="toggleTab">
		   <i class="material-icons md-36 md-violet">access_time</i>
		   <span>Timer</span>
		  </mat-button-toggle>
		</mat-button-toggle-group>
    </form>
  </mat-step>
  <mat-step>
    <form>
      <ng-template matStepLabel>Szene benennen & beschreiben</ng-template>
      <mat-form-field>
        <input matInput placeholder="Name" required>
      </mat-form-field>
      <mat-form-field>
        <input matInput placeholder="Beschreibung" required>
      </mat-form-field>
  <!--     <div>
        <button mat-button matStepperNext></button>
      </div> -->
    </form>
  </mat-step>
  
  <mat-step>
    <form>
      <ng-template matStepLabel>Auslöser</ng-template>
      <mat-form-field>
			  <mat-select class="selectPanel" placeholder="Auslöser">
			  <!--   <mat-option>-- None --</mat-option> -->
			   <!--  <mat-optgroup *ngFor="let modules of ApiService.modulesForDevices" [label]="modules.modulename" -->
			   	<mat-optgroup label="Vorkonfigurierte Auslöser" >
			   		<mat-option class="preconfigOption1" value="test">
				        Alle Fensterkontakte
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Bewegungsmelder
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Rauchmelder
				      </mat-option>
				      <mat-option class="preconfigOption1"value="test">
				        Alle Wassermelder
				      </mat-option>
			   	</mat-optgroup>

			      <div *ngFor="let modules of ApiService.modulesForDevices" >
				      <mat-option  class="matOption" *ngIf="modules.type === 2" [value]="modules.id">
				        {{modules.location}} - {{modules.description}}
				      </mat-option>
			      </div>

			<!--     </mat-optgroup> -->
			 </mat-select>

			</mat-form-field>
    </form>
  </mat-step>
  <mat-step>
    <form>
      <ng-template matStepLabel>Aktor</ng-template>
      <mat-form-field>
			  <mat-select  class="selectPanel" placeholder="Aktor">
			  <!--   <mat-option>-- None --</mat-option> -->
			   <!--  <mat-optgroup *ngFor="let modules of ApiService.modulesForDevices" [label]="modules.modulename" -->
			                 
			      <mat-option *ngFor="let modules of ApiService.modulesForDevices" [value]="modules.id">
			        {{modules.location}} {{modules.description}}
			      </mat-option>
			<!--     </mat-optgroup> -->
			 </mat-select>
			</mat-form-field>
    </form>
  </mat-step>
  <mat-step>
    <ng-template matStepLabel>Fertig</ng-template>
    Fertig?
    <!-- <div>
      <button mat-button matStepperPrevious>Zurück</button>
      <button mat-button (click)="stepper.reset()">Speichern</button>
    </div> -->
  </mat-step>
</mat-vertical-stepper>
	</mat-expansion-panel>




	<div class="alternate-theme"><button mat-stroked-button class="button1 " color="primary">neue Szene</button></div>
</div>

我想过滤所有时间戳。例如,这给了我我想要的东西:

ser = pd.Series(pd.date_range('2017/01/05', '2018/01/05'))
ser.loc[3] = 4
type(ser.loc[0])
> pandas._libs.tslibs.timestamps.Timestamp

但是我认为使用向量化解决方案会更快,并且避免使用ser.apply(lambda x: isinstance(x, pd.Timestamp)) 0 True 1 True 2 True 3 False 4 True ... 。我以为我应该可以使用apply

where

但是我明白了

  

ValueError:条件数组的形状必须与self相同

有没有办法做到这一点?另外,我以为它会更快/更“ Pandasic”正确吗?

2 个答案:

答案 0 :(得分:4)

这取决于数据的长度,但是对于小数据(365行),列表理解速度更快:

In [108]: %timeit (ser.apply(lambda x: isinstance(x, pd.Timestamp)))
434 µs ± 57.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [109]: %timeit ([isinstance(x, pd.Timestamp) for x in ser])
140 µs ± 5.09 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [110]: %timeit (pd.to_datetime(ser, errors='coerce').notna())
1.01 ms ± 25.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

但是如果测试更大的DataFrame更快to_datetime,并且通过Series.isna测试无缺失值:

ser = pd.Series(pd.date_range('1980/01/05', '2020/01/05'))
ser.loc[3] = 4

print (len(ser))
14611

In [116]: %timeit (ser.apply(lambda x: isinstance(x, pd.Timestamp)))
6.42 ms ± 541 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [117]: %timeit ([isinstance(x, pd.Timestamp) for x in ser])
4.9 ms ± 256 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [118]: %timeit (pd.to_datetime(ser, errors='coerce').notna())
4.22 ms ± 167 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

答案 1 :(得分:1)

要解决过滤问题,您可以转换为datetime并删除NaN。

ser[pd.to_datetime(ser, errors='coerce').notna()]

或者,如果您不介意结果是日期时间,

pd.to_datetime(ser, errors='coerce').dropna()