Skip to content

Commit bc25d2f

Browse files
committed
Added support for period index
1 parent e575ffb commit bc25d2f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

influxdb/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ def send_packet(self, packet):
742742
self.udp_socket.sendto(byte, (self._host, self.udp_port))
743743

744744
def write_points_from_dataframe(self, dataframe, name):
745-
dataframe = dataframe.copy()
745+
dataframe.index = dataframe.index.to_datetime()
746746
dataframe['time'] = [time.mktime(dt.timetuple()) for dt in dataframe.index]
747747
data = dict()
748748
data['name'] = name

tests/influxdb/client_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,39 @@ def test_write_points_from_dataframe(self):
629629
self.dummy_points
630630
)
631631

632+
def test_write_points_from_dataframe_with_period_index(self):
633+
now = datetime(2014, 11, 16)
634+
self.dummy_points = [
635+
{
636+
"points": [
637+
["1", 1, 1.0, time.mktime(now.timetuple())],
638+
["2", 2, 2.0, time.mktime((now + timedelta(hours=24)).timetuple())]
639+
],
640+
"name": "foo",
641+
"columns": ["column_one", "column_two", "column_three", "time"]
642+
}
643+
]
644+
self.dummy_dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]],
645+
index = [pd.Period('2014-11-16'), pd.Period('2014-11-17')],
646+
columns=["column_one", "column_two", "column_three"])
647+
648+
with requests_mock.Mocker() as m:
649+
m.register_uri(
650+
requests_mock.POST,
651+
"http://localhost:8086/db/db/series"
652+
)
653+
654+
cli = InfluxDBClient(database='db')
655+
cli.write_points_from_dataframe(
656+
self.dummy_dataframe, name="foo"
657+
)
658+
659+
self.assertListEqual(
660+
json.loads(m.last_request.body),
661+
self.dummy_points
662+
)
663+
664+
632665
def test_query_into_dataframe(self):
633666
data = [
634667
{

0 commit comments

Comments
 (0)