~awuerl/blitzortung-python/master

« back to all changes in this revision

Viewing changes to blitzortung/builder/base.py

  • Committer: Andreas Würl
  • Date: 2016-09-14 20:39:51 UTC
  • mto: This revision was merged to the branch mainline in revision 392.
  • Revision ID: git-v1:0577158d36adaee55ba3f8a63918b601e8a4edd4
remove pandas dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
"""
20
20
 
21
 
import datetime
22
 
 
23
 
import pytz
24
 
import numpy as np
25
 
import pandas as pd
26
 
 
27
21
from .. import data, Error
28
22
 
29
23
 
36
30
 
37
31
 
38
32
class Timestamp(Base):
39
 
    timestamp_string_minimal_fractional_seconds_length = 20
40
 
    timestamp_string_microseconds_length = 26
41
 
 
42
33
    def __init__(self):
43
34
        super(Timestamp, self).__init__()
44
35
        self.timestamp = None
45
36
 
46
 
    def set_timestamp(self, timestamp, nanoseconds=0):
 
37
    def set_timestamp(self, timestamp, nanosecond=0):
47
38
        if not timestamp:
48
39
            self.timestamp = None
49
 
        elif type(timestamp) == pd.Timestamp:
50
 
            if nanoseconds:
51
 
                self.timestamp = pd.Timestamp(timestamp.value + nanoseconds, tz=timestamp.tzinfo)
52
 
            else:
53
 
                self.timestamp = timestamp
54
 
        elif type(timestamp) == datetime.datetime:
55
 
            total_nanoseconds = pd.Timestamp(timestamp).value + nanoseconds
56
 
            self.timestamp = pd.Timestamp(total_nanoseconds, tz=timestamp.tzinfo)
 
40
        elif type(timestamp) == data.Timestamp:
 
41
            self.timestamp = timestamp + nanosecond
57
42
        else:
58
 
            self.timestamp = self.__parse_timestamp(timestamp)
 
43
            self.timestamp = data.Timestamp(timestamp, nanosecond=nanosecond)
59
44
        return self
60
45
 
61
 
    @staticmethod
62
 
    def __parse_timestamp(timestamp_string):
63
 
        try:
64
 
            timestamp = np.datetime64(timestamp_string + 'Z', 'ns')
65
 
            return pd.Timestamp(timestamp, tz=pytz.UTC)
66
 
        except ValueError:
67
 
            return pd.NaT
68
 
 
69
46
    def build(self):
70
47
        return self.timestamp
71
48
 
86
63
 
87
64
    def build(self):
88
65
        return data.Event(self.timestamp, self.x_coord, self.y_coord)
89
 
 
90
 
 
91